相關(guān)函數(shù)說明:
iconv
命令是用來轉(zhuǎn)換文件的編碼方式的,比如它可以將UTF8編碼的轉(zhuǎn)換成GB18030的編碼,反過來也行。
str_split()
函數(shù)把字符串分割到數(shù)組中。
bin2hex()
函數(shù)把 ASCII 字符的字符串轉(zhuǎn)換為十六進(jìn)制值。字符串可通過使用 pack() 函數(shù)再轉(zhuǎn)換回去。
hexdec()
函數(shù)把十六進(jìn)制數(shù)轉(zhuǎn)換為十進(jìn)制數(shù)。
免費(fèi)視頻教程推薦:php視頻教程
示例如下:
/** * $str 原始中文字符串 * $encoding 原始字符串的編碼,默認(rèn)GBK * $prefix 編碼后的前綴,默認(rèn)"&#" * $postfix 編碼后的后綴,默認(rèn)";" */ function unicode_encode($str, $encoding = 'GBK', $prefix = '&#', $postfix = ';') { $str = iconv($encoding, 'UCS-2', $str); $arrstr = str_split($str, 2); $unistr = ''; for($i = 0, $len = count($arrstr); $i < $len; $i++) { $dec = hexdec(bin2hex($arrstr[$i])); $unistr .= $prefix . $dec . $postfix; } return $unistr; }
相關(guān)文章教程推薦:php教程