Many PHP beginners use the substr() function or the mb_substr() function to intercept characters. The first Chinese character must be garbled, and the second one has poor performance. I have summarized a few custom ones below. Example of intercepting Chinese text strings without garbled characters.
Example 1
The code is as follows
代码如下 |
复制代码 |
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
if(function_exists("mb_substr"))
return mb_substr($str, $start, $length, $charset);
elseif(function_exists('iconv_substr')) {
return iconv_substr($str,$start,$length,$charset);
}
$re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
$re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
$re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
$re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
if($suffix) return $slice."…";
return $slice;
}
|
|
Copy code
|
代码如下 |
复制代码 |
//$start:指定开始截取字符串的位置;$length指定截取字符的长度
function substr2($string, $start, $length)
{
$len = strlen($string);
if($len > $length)
{
$str = '';
$len1 = $start + $length; //截取到原字符串的位置
for($i=$start; $i<$len1; $i++)
{
if(ord(substr($string, $i, 2)) > 0xa0) //在ASCII中,0xa0表示汉字的开始
{
$str.=substr($string, $i, 2);
$i++;
}
else
{
$str.=substr($string, $i, 1);
}
}
return $str.'...';
}
else
{
return $string;
}
}
?>
|
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
If(function_exists("mb_substr"))
代码如下 |
复制代码 |
function chinesesubstr($str, $start, $len){
$strlen = $start + $len;
for($i=0; $i<$strlen; $i++){
if(ord(substr($str, $i, 1)) > 0xa0){
$tmpstr .= substr($str, $i, 2);
$i++;
}else{
$tmpstr .= substr($str, $i, 1);
}
}
return $tmpstr;
}
$str = "waiting for you 等wait你back";
echo chinesesubstr($str, 0, 19)
?>
|
return mb_substr($str, $start, $length, $charset); |
elseif(function_exists('iconv_substr')) {
return iconv_substr($str,$start,$length,$charset);
}
$re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80 -xbf]{3}/";
$re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
$re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
$re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
Preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
If($suffix) return $slice."…";
return $slice;
}
Example 2
The code is as follows
|
Copy code
|
//$start: Specifies the starting position to intercept the string; $length specifies the length of the intercepted characters <🎜>
function substr2($string, $start, $length)<🎜>
{<🎜>
$len = strlen($string);<🎜>
if($len > $length)
{
$str = '';
$len1 = $start + $length; //Intercept to the position of the original string
for($i=$start; $i<$len1; $i++)<🎜>
{<🎜>
If(ord(substr($string, $i, 2)) > 0xa0) //In ASCII, 0xa0 represents the beginning of Chinese characters
{
$str.=substr($string, $i, 2);
$i++;
}
else
{
$str.=substr($string, $i, 1);
}
}
Return $str.'...';
}
else
{
Return $string;
}
}
?>
I’ll add a simple one with the same idea (2010-5-31)
The code is as follows
|
Copy code
|
function chinesesubstr($str, $start, $len){<🎜>
$strlen = $start + $len;<🎜>
for($i=0; $i<$strlen; $i++){<🎜>
If(ord(substr($str, $i, 1)) > 0xa0){
$tmpstr .= substr($str, $i, 2);
$i++;
}else{
$tmpstr .= substr($str, $i, 1);
}
}
Return $tmpstr;
}
$str = "waiting for you wait for you back";
echo chinesesubstr($str, 0, 19)
?>
http://www.bkjia.com/PHPjc/632772.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632772.htmlTechArticleMany PHP beginners use the substr() function or mb_substr() function to intercept characters. Chapter One Chinese character must be garbled, and the second one has poor performance. I have summarized a few customizations below...
|
|