English | 简体中文 | 繁體中文
查询

recode()函数—用法及示例

「 将字符串从一种字符编码转换为另一种字符编码 」


函数名:recode() 

函数描述:将字符串从一种字符编码转换为另一种字符编码。

用法: string recode ( string $in_charset , string $out_charset , string $str )

参数:

  • $in_charset:输入字符串的字符编码。
  • $out_charset:输出字符串的字符编码。
  • $str:要转换的字符串。

返回值: 返回转换后的字符串,如果转换失败则返回 FALSE。

注意事项:

  • recode 函数需要 PHP 安装 recode 扩展才能使用。可以通过在编译 PHP 时加入 --with-recode[=DIR] 参数来启用 recode 扩展。
  • recode 扩展支持的字符编码列表可以通过 recode_string("...") 函数获取。

示例:

// 转换 ISO-8859-1 编码的字符串为 UTF-8 编码
$str = "Hello, world!";
$converted_str = recode("ISO-8859-1", "UTF-8", $str);
echo $converted_str;  // 输出:Hello, world!

// 转换 UTF-8 编码的字符串为 ASCII 编码
$str = "你好,世界!";
$converted_str = recode("UTF-8", "ASCII", $str);
echo $converted_str;  // 输出:Ni Hao , Shi Jie !

注意:recode 函数在 PHP 7.4 版本中已被弃用,推荐使用 iconv() 函数来进行字符编码转换。

补充纠错
上一个函数: recode_file()函数
下一个函数: realpath_cache_size()函数
热门PHP函数
分享链接