函数名称:recode_string()
函数描述:该函数用于将字符串转换为指定字符集的编码。
适用版本:PHP 4 >= 4.0.5, PHP 5, PHP 7
语法:string recode_string ( string $request , string $string )
参数:
- $request:要转换的字符集,可以是字符串或字符串数组。
- $string:要转换的字符串。
返回值:返回转换后的字符串,如果转换失败则返回 FALSE。
示例:
// 将字符串从 ISO-8859-1 转换为 UTF-8
$input = "Hello, World!";
$output = recode_string("iso-8859-1..utf-8", $input);
echo $output; // 输出:Hello, World!
// 将字符串从 GB2312 转换为 UTF-8
$input = "你好,世界!";
$output = recode_string("gb2312..utf-8", $input);
echo $output; // 输出:你好,世界!
注意事项:
- recode_string() 函数需要安装 recode 扩展库才能正常工作。
- 字符集的转换格式需要符合 recode 扩展库的支持列表,否则转换会失败。
- 如果转换失败,recode_string() 函数会返回 FALSE,因此在使用转换结果之前需要进行错误检查。