Home > Article > Backend Development > Intercepting a string with a specified number of characters is valid in Chinese_PHP tutorial
function csubStr($str,$start,$len)
{
$strlen=strlen($str);
$clen=0;
for($i=0;$ i{
if ($clen>=$start+$len)
break;
if(ord(substr($str,$i,1 ))>0xa0)
{
if ($clen>=$start)
$tmpstr.=substr($str,$i,2);
$i++;
}
else
{
if ($clen>=$start)
$tmpstr.=substr($str,$i,1);
}
}
return $ tmpstr;
}
Example: $str = csubStr("Hello everyone, intercept the string with the specified number of characters, 886!",0,10);
can usually be used to display the forum topic list, such as with The following function:
function showShort($str,$len)
{
if($len {
$tempStr = csubStr( $str,0,$len);
$tempStr .= "...";
}
else
{
$tempStr =$str;
}
return $tempStr;
}
If the topic exceeds the specified number of words, it will be intercepted and then added with ellipses. Otherwise, the entire word count is displayed.
eclipse replied on: 2001-11-24 21:39:31 [b][color=red] Not much use, right?[/color][/b]
bigmouse replied on: 2001-11-29 12:02:54 It is still useful. If you intercept Chinese characters without judging them, garbled characters will appear when you intercept the middle of the Chinese characters. After all, Chinese characters are double bytes, so you need to make a special judgment.
evil replied at: 2002-11-12 11:42:07 mb_substr can do it