php实现utf-8转unicode函数分享
代码很简单,功能却很实用,推荐给大家。
奉上代码先:
publicfunctionutf8_unicode($str){
$unicode=array();
$values=array();
$lookingFor=1;
for($i=0;$i<strlen($str);$i++){
$thisValue=ord($str[$i]);
if($thisValue<ord('A')){
//exclude0-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
returnimplode("",$unicode);
}
热门推荐
10 五一公司放假祝福语简短
11 追星女孩的祝福语简短
12 给长辈端午祝福语 简短
13 生日祝福语有诗意简短
14 团圆日祝福语简短
15 中秋商务祝福语简短最新
16 温暖文艺简短祝福语短句
17 男孩上学祝福语简短的
18 入住酒店文案祝福语简短