温馨提示×

php 中文unicode 互转

PHP
小云
125
2023-10-11 11:56:46
栏目: 编程语言

在 PHP 中,可以使用 mb_convert_encoding() 函数进行中文和 Unicode 之间的互转。

要将中文转换为 Unicode,可以使用以下代码:

$chinese = "中文";
$unicode = mb_convert_encoding($chinese, 'unicode');
echo $unicode; // 输出:\u4e2d\u6587

要将 Unicode 转换为中文,可以使用以下代码:

$unicode = '\u4e2d\u6587';
$chinese = mb_convert_encoding($unicode, 'utf-8', 'unicode');
echo $chinese; // 输出:中文

注意,mb_convert_encoding() 函数的第一个参数是要转换的字符串,第二个参数是目标编码,第三个参数是原始编码。在上面的示例中,目标编码为 UTF-8,原始编码为 Unicode。

0