php實現utf-8轉unicode函數分享
更新時間:2015年01月06日 14:51:37 投稿:hebedich
這篇文章主要介紹了php實現utf-8轉unicode函數分享,需要的朋友可以參考下
代碼很簡單,功能卻很實用,推薦給大家。
奉上代碼先:
復制代碼 代碼如下:
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);
}
相關文章
Laravel框架學習筆記(二)項目實戰(zhàn)之模型(Models)
上一篇已經介紹開發(fā)環(huán)境的搭建,這篇將從項目實戰(zhàn)開發(fā),一步一步了解laravel框架。首先我們來了解下laravel框架的模型 (Models)2014-10-10Laravel 5框架學習之Eloquent (laravel 的ORM)
Laravel 的 Eloquent ORM 提供了漂亮、簡潔的 ActiveRecord 實現來和數據庫的互動。 每個數據庫表會和一個對應的「模型」互動。在開始之前,記得把 config/database.php 里的數據庫連接配置好。2015-04-04PHP中Fatal error session_start()錯誤解決步驟
這篇文章主要介紹了PHP中Fatal error session_start()錯誤解決步驟,著重于錯誤的排除步驟,一步一步排查下去,肯定可以解決這個錯誤,需要的朋友可以參考下2014-08-08