Copy code The code is as follows:
function msubstr($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;
}
Copy code The code is as follows:
< ;?PHP
$str="This character is so long,^_^";
$Short_Str=showShort($str,4);//Intercept the first 4 Chinese characters, the result is: this character.. .
Echo "$Short_Str";
Function csubstr($str,$start,$len)
{
$strlen=strlen($str);
$clen=0;
for($i=0;$i<$strlen;$i++,$clen++)
{
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;
}
Function showShort($str,$len)
{
$tempstr = csubstr($str,0,$ len);
if ($str<>$tempstr)
$tempstr .= "..."; //Whatever you want to end with, just modify it here.
return $tempstr ;
}
How about the nagging method, concise?
Copy code The code is as follows:
$len = 19;
$text = "How to display only the first few words of a long news title and replace it with...? ";
echo strlen($text)<=$len ? $text : (substr($text,0,$len).chr(0)."....");
http://www.bkjia.com/PHPjc/770586.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/770586.htmlTechArticleCopy the code The code is as follows: function msubstr($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++; }...