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:03:58
Original
712 people have browsed it

Example of PHP interception Chinese string function

This article mainly introduces PHP interception Chinese string function. The example analyzes PHP's skills for Chinese string operations. Pay attention to utf- The conversion problem between 8 and gb2312 encoding has certain reference value. Friends in need can refer to it

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:

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 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.

www.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...
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