php截取中文字符串的两种方式

PHP 2013-03-01

直接上源码。代码里面有解释,适用于UTF-8编码模式

$str的字符个数,造成无谓的while循环,($offset<$length限定)
function utf8sub($str,$len){
    if($len<=0){
        return ;
    }
    $res="";
    $offset=0;
    $chars=0;
    $length=strlen($str);
    while($chars<$len && $offset<$length){
    
        $hign=decbin(ord(substr($str,$offset,1)));
            if(strlen($hign)<8){
                $count=1;
            }elseif(substr($hign,0,3)=="110"){
                $count=2;
            }elseif(substr($hign,0,4)=="1110"){
                $count=3;
            }elseif(substr($hign,0,5)=="11110"){
                $count=4;
            }elseif(substr($hign,0,6)=="111110"){
                $count=5;
            }elseif(substr($hign,0,7)=="1111110"){
                $count=6;
            }
        
        $res.=substr($str,$offset,$count);
        $offset+=$count;
        $chars+=1;
        
        
    }
    return $res;
}
function utf8sub1($str,$len){
    $chars=0;
    $res="";
    $offset=0;
    $length=strlen($str);
    while($chars<$len && $offset<$length){
        $hign=decbin(ord(substr($str,$offset,1)));
        if(strlen($hign)<8){
            $count=1;
        }elseif($hign & "11100000"=="11000000"){
            $count=2;
        }elseif($hign & "11110000"=="11100000"){
            $count=3;
        }elseif($hign & "11111000"=="11110000"){
            $count=4;
        }elseif($hign & "11111100"=="11111000"){
            $count=5;
        }elseif($hign & "11111110"=="11111100"){
            $count=6;
        }
        $res.=substr($str,$offset,$count);
        $chars++;
        $offset+=$count;
    }
    return $res;
}
$a="一二ah三四hdj";
echo utf8sub($a,5);
?>

本文由 Jazzy 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论