Home > Backend Development > PHP Tutorial > php字符串截取中文截取2,单字节截取模式_php技巧

php字符串截取中文截取2,单字节截取模式_php技巧

WBOY
Release: 2016-05-17 09:38:50
Original
1106 people have browsed it

//中文截取2,单字节截取模式
function cn_substr($str,$slen,$startdd=0){
    $restr = "";
    $c = "";
    $str_len = strlen($str);
    if($str_len     if($str_len     $enddd = $startdd + $slen - 1;
    for($i=0;$i    {
        if($startdd==0) $restr .= $c;
        else if($i > $startdd) $restr .= $c;

        if(ord($str[$i])>0x80){
            if($str_len>$i+1) $c = $str[$i].$str[$i+1];
            $i++;
        }
        else{    $c = $str[$i]; }

        if($i >= $enddd){
            if(strlen($restr)+strlen($c)>$slen) break;
            else{ $restr .= $c; break; }
        }
    }
    return $restr;
}
function cn_midstr($str,$start,$len){
    return cn_substr($str,$slen,$startdd);
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template