-
- 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
Call:
-
-
$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++;
- } bbs.it-home.org
- else
- {
- if ($clen>=$start)
- $tmpstr.=substr($str,$i,1);
- }
- }
return $tmpstr;
- }
- Function showShort($ str,$len)
- {
- $tempstr = csubstr($str,0,$len);
- if ($str<>$tempstr)
- $tempstr .= "..."; //What to end with , just modify it here.
return $tempstr;
- }
-
Copy the code
Here we share a more concise method of intercepting the length of Chinese characters:
-
-
- $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)."....");
Copy code
|