Home >php教程 >php手册 >php字符串截取中文截取2,单字节截取模式

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

WBOY
WBOYOriginal
2016-06-13 12:30:381295browse

//中文截取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);
}

Statement:
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