Home > php教程 > php手册 > body text

比较完善的php取字符函数

WBOY
Release: 2016-06-13 10:57:09
Original
831 people have browsed it

用了几个取字符函数mb_substr之类的都不怎么好用,还是这个比较好用一点。


[php] 
/**
 * 截断函数,防止乱码
 *
 *
 */  
function z_substr($sourcestr='',$i=0,$cutlength=150,$endstr='')  
{  
    $str_length=strlen($sourcestr);//字符串的字节数   
    while (($n     {  
        $temp_str=substr($sourcestr,$i,1);  
        $ascnum=Ord($temp_str);//ascii码   
        if ($ascnum>=224)  
        {  
            $returnstr=$returnstr.substr($sourcestr,$i,3);  
            $i=$i+3;  
            $n++;  
        }elseif ($ascnum>=192)  
        {  
            $returnstr=$returnstr.substr($sourcestr,$i,2);  
            $i=$i+2;  
            $n++;  
        }else  
        {  
            $returnstr=$returnstr.substr($sourcestr,$i,1);  
            $i=$i+1;  
            $n=$n+0.5;  
        }  
    }  
    if($i     return $returnstr;  
}  

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template