溫馨提示×

php 中文unicode 互轉(zhuǎn)

PHP
小云
249
2023-10-11 11:56:46
欄目: 編程語言

在 PHP 中,可以使用 mb_convert_encoding() 函數(shù)進行中文和 Unicode 之間的互轉(zhuǎn)。

要將中文轉(zhuǎn)換為 Unicode,可以使用以下代碼:

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

要將 Unicode 轉(zhuǎn)換為中文,可以使用以下代碼:

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

注意,mb_convert_encoding() 函數(shù)的第一個參數(shù)是要轉(zhuǎn)換的字符串,第二個參數(shù)是目標編碼,第三個參數(shù)是原始編碼。在上面的示例中,目標編碼為 UTF-8,原始編碼為 Unicode。

0