Home > Backend Development > PHP Tutorial > PHP interception Chinese string function example, _PHP tutorial

PHP interception Chinese string function example, _PHP tutorial

WBOY
Release: 2016-07-13 10:06:19
Original
810 people have browsed it

PHP interception Chinese string function example,

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:

Copy code The code 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 the right foot is so cute";
echo substr_zh($string,10,0,'gb2312');
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/959883.htmlTechArticlePHP interception Chinese string function example, this article describes the php interception Chinese string function example. Share it with everyone for your reference. The specific implementation method is as follows: Copy the code The code is as follows...
Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template