PHP版的charCodeAt()

charCodeAt() 方法可返回指定位置的字符的 Unicode 编码。这个返回值是 0 - 65535 之间的整数。

很遗憾PHP中没有这个方法.

 

function uniord($str,$from_encoding=false){
        $from_encoding=$from_encoding ? $from_encoding : 'UTF-8';

        if(strlen($str)==1){ return ord($str);} 
               
                
        $str=mb_convert_encoding($str, 'UCS-4BE', $from_encoding);
        $tmp=unpack('N',$str);
        return $tmp[1];
}

$str="12345,上山打老虎";

$result=array();
for($i=0,$l=mb_strlen($str,'utf-8');$i<$l;++$i){
        $result[]=uniord(mb_substr($str,$i,1,'utf-8'));
}
echo join(",",$result);

 

posted @ 2014-02-27 16:40  tlijian1989  阅读(1918)  评论(0编辑  收藏  举报