PHP 中解碼 Unicode 編碼的字符串,將正常的字符串編碼為 Unicode 編碼

要在 PHP 中解碼 Unicode 編碼的字符串,你可以使用 json_decode() 函數。這裡是一個簡單的例子:

<?php

$unicode_string = '\"\\u4f60\\u597d\\uff0c\\u4e16\\u754c\\uff01\"';
$decoded_string = json_decode($unicode_string);

echo $decoded_string; // 輸出:你好,世界!

?>

在這個例子中,$unicode_string 包含一個 Unicode 編碼的字符串(在這種情況下是 “你好,世界!”)。我們使用 json_decode() 函數將其解碼為正常的 UTF-8 字符串。請注意,字符串必須用雙引號括起來,以便 json_decode() 能夠正確解析它。

如果你需要將正常的字符串編碼為 Unicode 編碼,可以使用 json_encode() 函數。下面是一個示例:

<?php

$string = "你好,世界!";
$encoded_string = json_encode($string);

echo $encoded_string; // 輸出:"\u4f60\u597d\uff0c\u4e16\u754c\uff01"

?>

在這個例子中,我們使用 json_encode() 函數將 UTF-8 字符串編碼為 Unicode 編碼格式。

發布者:彬彬筆記,轉載請註明出處:https://www.binbinbiji.com/zh-hant/php/3093.html

(0)
彬彬筆記彬彬筆記
上一篇 2023年3月27日
下一篇 2023年3月28日

相關推薦

發表回復

登錄後才能評論
蜀ICP備14017386號-13