Home  >  Article  >  Backend Development  >  A relatively complete PHP character extraction function_PHP tutorial

A relatively complete PHP character extraction function_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:11:10834browse

I have used several character-retrieving functions such as mb_substr, but they are not very easy to use. This one is better.


[php]
/**
* Truncate functions to prevent garbled characters
*
*
​*/
function z_substr($sourcestr='',$i=0,$cutlength=150,$endstr='')
{
$str_length=strlen($sourcestr);//Number of bytes of string
while (($n<$cutlength) and ($i<=$str_length))

          $temp_str=substr($sourcestr,$i,1);                                              $ascnum=Ord($temp_str);//ascii code                                                                                    If ($ascnum>=224)
                                                                                       $returnstr=$returnstr.substr($sourcestr,$i,3);
                      $i=$i+3;                                                                                                       }elseif ($ascnum>=192)  
                                                                                       $returnstr=$returnstr.substr($sourcestr,$i,2);
                      $i=$i+2;                                                                                                        }else 
                                                                                       $returnstr=$returnstr.substr($sourcestr,$i,1);
                $i=$i+1;                                                                  $n=$n+0.5;                                                                                    }  
If($i<$str_length)$returnstr.=$endstr;
Return $returnstr;
}




http://www.bkjia.com/PHPjc/477395.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/477395.html

TechArticle

I have used several character-retrieving functions such as mb_substr, but they are not very easy to use. This one is better. [php] /*** Truncate functions to prevent garbled characters * **/ function z_substr($sourcestr=,$i=...

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