Function name: mb_convert_encoding()
Applicable version: PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8
Function Description: mb_convert_encoding() function converts a string from one character encoding to another character encoding.
Usage: string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding = mb_internal_encoding() ] ) : string|false
parameter:
- $str: Encoded strings need to be converted.
- $to_encoding: The target character encoding can be a character encoding string or a character encoding array.
- $from_encoding: original character encoding, optional parameters. If not specified, the character encoding returned by the mb_internal_encoding() function is defaulted to use.
Return value:
- If the conversion is successful, the converted string is returned.
- If the conversion fails, false is returned.
Example:
// 将一个字符串从ISO-8859-1 编码转换为UTF-8 编码$str = "Hello, 你好!"; $convertedStr = mb_convert_encoding($str, "UTF-8", "ISO-8859-1"); echo $convertedStr; // 输出:Hello, 你好! // 将一个字符串从UTF-8 编码转换为GBK 编码$str = "Hello, 你好!"; $convertedStr = mb_convert_encoding($str, "GBK", "UTF-8"); echo $convertedStr; // 输出:Hello, 你好!(在GBK编码下显示)
Notes:
- This function relies on a multibyte string extension, which needs to be enabled in the PHP configuration.
- If no original character encoding is specified, the character encoding returned by the mb_internal_encoding() function is used by default.
- If the specified character encoding is unavailable or not supported, the conversion fails and returns false.
- This function can convert a string from one character encoding to multiple target character encodings at one time. If the $to_encoding parameter is a character-encoded array, the function returns an array containing multiple converted strings.