HomeLanguagesPHP mb_convert_encoding() Function

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:

  • 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

RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS