Open In App

PHP mb_convert_encoding() Function

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:

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
  
// 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
  
// 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


Article Tags :