本文實例講述了php自定義函數實現漢字轉換utf8編碼的方法。分享給大家供大家參考,具體如下:
該函數有兩部分組成:
function htou($c) { $n = (ord($c[0]) & 0x1f) << 12; $n += (ord($c[1]) & 0x3f) << 6; $n += ord($c[2]) & 0x3f; return $n;}//在代碼中隱藏utf8格式的字符串function utf8_unicode($str) { $encode=''; for($i=0;$i<strlen($str);$i++) { if(ord(substr($str,$i,1))> 0xa0) { $encode.='&#'.htou(substr($str,$i,3)).';'; $i+=2; } else { $encode.='&#'.ord($str[$i]).';'; } } return $encode;}我們可以用 utf8_unicode() 就可以轉換漢字,返回為漢字的utf8編碼。這樣輸出網頁不會是亂碼。
希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選