php 中文unicode 互转
在 PHP 中,可使用 mb_convert_encoding()
函数进行中文和 Unicode 之间的互转。
要将中文转换为 Unicode,可使用以下代码:
$chinese = "中文";
$unicode = mb_convert_encoding($chinese, 'unicode');
echo $unicode; // 输出:u4e2du6587
要将 Unicode 转换为中文,可使用以下代码:
$unicode = 'u4e2du6587';
$chinese = mb_convert_encoding($unicode, 'utf⑻', 'unicode');
echo $chinese; // 输出:中文
注意,mb_convert_encoding()
函数的第一个参数是要转换的字符串,第二个参数是目标编码,第三个参数是原始编码。在上面的示例中,目标编码为 UTF⑻,原始编码为 Unicode。
TOP