問(wèn)題描述
是否有將 UTF-8 轉(zhuǎn)換為 Unicode 而將非特殊字符保留為普通字母和數(shù)字的函數(shù)?
Is there a function that will change UTF-8 to Unicode leaving non special characters as normal letters and numbers?
即德語(yǔ)單詞tchü?"將被呈現(xiàn)為類(lèi)似于tch20AC21AC"(請(qǐng)注意我正在制作 Unicode 代碼).
ie the German word "tchü?" would be rendered as something like "tch20AC21AC" (please note that I am making the Unicode codes up).
我正在試驗(yàn)以下函數(shù),但盡管這個(gè)函數(shù)適用于 ASCII 32-127,但對(duì)于雙字節(jié)字符似乎失敗了:
I am experimenting with the following function, but although this one works well with ASCII 32-127, it seems to fail for double byte chars:
function strToHex ($string)
{
$hex = '';
for ($i = 0; $i < mb_strlen ($string, "utf-8"); $i++)
{
$id = ord (mb_substr ($string, $i, 1, "utf-8"));
$hex .= ($id <= 128) ? mb_substr ($string, $i, 1, "utf-8") : "&#" . $id . ";";
}
return ($hex);
}
有什么想法嗎?
編輯 2:找到解決方案:PHP ord() 函數(shù)不適用于雙字節(jié)字符.改用:http://nl.php.net/manual/en/function.ord.php#78032
EDIT 2: Found solution: The PHP ord() function does not work for double byte chars. Use instead: http://nl.php.net/manual/en/function.ord.php#78032
推薦答案
可以使用 iconv 將一種字符集轉(zhuǎn)換為另一種字符集:
Converting one character set to another can be done with iconv:
http://php.net/manual/en/function.iconv.php
請(qǐng)注意,UTF 已經(jīng)是 Unicode 編碼了.
Note that UTF is already an Unicode encoding.
另一種方法是簡(jiǎn)單地使用具有正確字符集的 htmlentities:
Another way is simply using htmlentities with the right character set:
http://php.net/manual/en/function.htmlentities.php
這篇關(guān)于UTF-8 到 Unicode 代碼點(diǎn)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!