Open In App

PHP mb_convert_encoding() Function

Last Updated : 28 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The mb_convert_encoding() function is an inbuilt function in PHP that transforms the string into another character encoding.

Syntax:

mb_convert_encoding(
    array|string $string, 
    string $to_encoding, 
    array|string|null 
    $from_encoding = null
): array|string|false

Parameters: This function has 3 parameters:

  • string: The parameter specifies the string or array that is to be converted.
  • to_encoding: This parameter specifies the desired encoding for the result.
  • from_encoding: If mbstring.internal_encoding setting will be utilized, if from_encoding is null or not specified, otherwise the default_charset setting will be used.

Return Value: This function returns an encoded string if the function is successfully executed otherwise it will return false.

Example 1: The following code demonstrates the mb_convert_encoding() function.

PHP




<?php
  
// UTF-8 string to be converted to ASCII
$string = "Café au lait";
  
// Convert UTF-8 string to ASCII
$ascii_string = mb_convert_encoding($string, "ASCII");
  
// Output the converted string
echo $ascii_string;
?>


Output:

Caf? au lait  

Example 2: The following code demonstrates the mb_convert_encoding() function.

PHP




<?php
  
// ISO-8859-1 string to be converted to UTF-8
$string = "Björk";
  
// Convert ISO-8859-1 string to UTF-8
$utf8_string = mb_convert_encoding(
    $string, "UTF-8", "ISO-8859-1");
  
// Output the converted string
echo $utf8_string;
?>


Output:

Björk       

Reference: https://www.php.net/manual/en/function.mb-convert-encoding.php



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads