This article introduces a function that comes with PHP to intercept strings. It can only handle English. Numbers cannot intercept mixed Chinese characters. Friends in need can refer to it. The latter one is easier to use. The first one is mainly for For beginners to learn from.
代码如下 |
复制代码 |
/*
------------------------------------------------------
参数:
$str_cut 需要截断的字符串
$length 允许字符串显示的最大长度
程序功能:截取全角和半角(汉字和英文)混合的字符串以避免乱码
------------------------------------------------------
*/
function substr_cut($str_cut,$length)
{
if (strlen($str_cut) > $length)
{
for($i=0; $i < $length; $i++)
if (ord($str_cut[$i]) > 128) $i++;
$str_cut = substr($str_cut,0,$i)."..";
}
return $str_cut;
}
?>
|
/*
-------------------------------------------------- ----
Parameters:
$str_cut The string that needs to be truncated
$length The maximum length allowed for string display
Program function: intercept mixed full-width and half-width (Chinese characters and English) strings to avoid garbled characters
-------------------------------------------------- ----
*/
function substr_cut($str_cut,$length)
{
If (strlen($str_cut) > $length)
{
for($i=0; $i < $length; $i++)
If (ord($str_cut[$i]) > 128) $i++;
$str_cut = substr($str_cut,0,$i)."..";
}
Return $str_cut;
}
?>
http://www.bkjia.com/PHPjc/445327.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445327.htmlTechArticle
This article introduces a function that comes with PHP to intercept strings. It can only handle English, but not numbers. Intercepted Chinese mixed arrangement, friends in need can refer to it, the latter one is easier to use,...