Current Location: Home> Function Categories> mb_convert_variables

mb_convert_variables

Convert character encoding of one or more variables
Name:mb_convert_variables
Category:Multi-byte string
Programming Language:php
One-line Description:Convert a variable from one character encoding to another

Function name: mb_convert_variables()

Applicable version: PHP 4 >= 4.0.6, PHP 5, PHP 7

Function Description: The mb_convert_variables() function converts a variable from one character encoding to another character encoding. It can be used to convert all string values ​​in an array or object.

Syntax: mb_convert_variables(string $to_encoding, mixed $from_encoding, mixed &$vars)

parameter:

  • $to_encoding: The target character encoding can be a string or a string array.
  • $from_encoding: Source character encoding, can be a string or a string array. If the parameter is an empty string or an array, the internally configured character encoding is used.
  • &$vars: The variable to be converted can be a string, array, or object. If an object is passed to this parameter, all its public member variables will be converted.

Return value: Returns the converted variable, and returns false if the conversion fails.

Example:

 // 转换字符串编码$str = "你好,世界!"; $newStr = mb_convert_variables('UTF-8', 'GBK', $str); echo $newStr; // 输出:你好,世界! // 转换数组中的字符串编码$arr = array("你好", "世界"); $newArr = mb_convert_variables('UTF-8', 'GBK', $arr); print_r($newArr); // 输出:Array ( [0] => 你好[1] => 世界) // 转换对象中的字符串编码class Example { public $str1 = "你好"; public $str2 = "世界"; } $obj = new Example(); mb_convert_variables('UTF-8', 'GBK', $obj); echo $obj->str1; // 输出:你好echo $obj->str2; // 输出:世界

Notes:

  • If the specified character encoding is invalid or not supported, the conversion will fail.
  • If the source character is encoded as an empty string or array, the internally configured character encoding is used.
  • The mb_convert_variables() function directly modifies the variable passed to it, rather than returning a new variable.
Similar Functions
Popular Articles