php實(shí)現(xiàn)utf-8轉(zhuǎn)unicode函數(shù)分享
更新時(shí)間:2015年01月06日 14:51:37 投稿:hebedich
這篇文章主要介紹了php實(shí)現(xiàn)utf-8轉(zhuǎn)unicode函數(shù)分享,需要的朋友可以參考下
代碼很簡(jiǎn)單,功能卻很實(shí)用,推薦給大家。
奉上代碼先:
復(fù)制代碼 代碼如下:
public function utf8_unicode($str) {
$unicode = array();
$values = array();
$lookingFor = 1;
for ($i = 0; $i < strlen( $str ); $i++ ) {
$thisValue = ord( $str[ $i ] );
if ( $thisValue < ord('A') ) {
// exclude 0-9
if ($thisValue >= ord('0') && $thisValue <= ord('9')) {
// number
$unicode[] = chr($thisValue);
}
else {
$unicode[] = '%'.dechex($thisValue);
}
} else {
if ( $thisValue < 128) {
$unicode[] = $str[ $i ];
} else {
if ( count( $values ) == 0 ) {
$lookingFor = ( $thisValue < 224 ) ? 2 : 3;
}
$values[] = $thisValue;
if ( count( $values ) == $lookingFor ) {
$number = ( $lookingFor == 3 ) ?
( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
$number = dechex($number);
$unicode[] = (strlen($number)==3)?"\u0".$number:"\u".$number;
$values = array();
$lookingFor = 1;
} // if
} // if
}
} // for
return implode("",$unicode);
}
您可能感興趣的文章:
- PHP解碼unicode編碼的中文字符代碼分享
- php utf-8轉(zhuǎn)unicode的函數(shù)
- 淺析PHP中的UNICODE 編碼與解碼
- PHP中正則表達(dá)式對(duì)UNICODE字符碼的匹配方法
- PHP如何實(shí)現(xiàn)Unicode和Utf-8編碼相互轉(zhuǎn)換
- php UTF-8、Unicode和BOM問(wèn)題
- 簡(jiǎn)單談?wù)刾hp中的unicode和utf8編碼
- php制作unicode解碼工具(unicode編碼轉(zhuǎn)換器)代碼分享
- 用php實(shí)現(xiàn)gb2312和unicode間的編碼轉(zhuǎn)換
- PHP解密Unicode及Escape加密字符串
- PHP實(shí)現(xiàn)Unicode編碼相互轉(zhuǎn)換的方法示例
相關(guān)文章
生成隨機(jī)字符串和驗(yàn)證碼的類(lèi)的PHP實(shí)例
這篇文章主要介紹了生成隨機(jī)字符串和驗(yàn)證碼的類(lèi)的PHP實(shí)例,有需要的朋友可以參考一下2013-12-12
Codeigniter通過(guò)SimpleXML將xml轉(zhuǎn)換成對(duì)象的方法
這篇文章主要介紹了Codeigniter通過(guò)SimpleXML將xml轉(zhuǎn)換成對(duì)象的方法,涉及Codeigniter操作XML文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
php獲取數(shù)據(jù)庫(kù)結(jié)果集方法(推薦)
下面小編就為大家?guī)?lái)一篇php獲取數(shù)據(jù)庫(kù)結(jié)果集方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

