Home  >  Article  >  php教程  >  比较完善的php取字符函数

比较完善的php取字符函数

WBOY
WBOYOriginal
2016-06-13 10:57:09804browse

用了几个取字符函数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;  
}  

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
Previous article:php实战第三天Next article:php实战第四天