The example in this article describes the function of intercepting Chinese strings in PHP. Share it with everyone for your reference. The specific implementation method is as follows:
//Chinese string interception
function substr_zh($string,$sublen,$start=0,$code='UTF-8'){
if($code=='UTF-8'){
$pa = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]| xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";
preg_match_all($pa,$string,$t_string);
if(count($t_string[0])-$start > $sublen){
return join('',array_slice($t_string[0],$start,$sublen))."...";
//array_slice() takes out a segment of value in the array based on conditions, parameters (array, starting position, [length])
}else{
return join('',array_slice($t_string[0],$start,$sublen));
}
}else{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0;$i<$strlen;$i++){
if($i>$start && $i<($start+$sublen)){
if(ord(substr($string,$i,1))>129){
//ord(): Returns the ASCII value of the first character of the string
//substr(): Returns a part of the string
$tmpstr .= substr($string,$i,2);
}else{
$tmpstr .= substr($string,$i,1);
}
}
if(ord(substr($string,$i,1))>129){
$i++;
}
if(strlen($tmpstr)<$strlen){
$tmpstr .= "...";
}
}
return $tmpstr;
}
}
$string ="The overhead car jacks up the dilemma. The Shanghai Association is afraid of the Wizards and is willing to be trapped. The right foot is cute.";
echo substr_zh($string,10,0,'gb2312');
?>
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/966928.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/966928.htmlTechArticlePHP interception Chinese string function example This article mainly introduces php interception Chinese string function, and the example analyzes php For tips on Chinese string operations, pay attention to the conversion of utf-8 and gb2312 encoding...