首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[函数]recode()函数—用法及示例

发布于 2025-05-03 12:50:56
0
34

函数名: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() 函数来进行字符编码转换。

评论
一个月内的热帖推荐
啊龙
Lv.1普通用户

9545

帖子

31

小组

3242

积分

赞助商广告
站长交流