- 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 CodeCall:
$str = "This character is very long, ^_^";
- $Short _str=showshort ($STR, 4);//intercept the first 4 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. = "..."; To what end, modify here can be.
return $tempstr;
- }
Copy CodeHere is a more concise way to intercept the length of Chinese characters:
- $len = 19;
- $text = "How will the long title of the news show only a few words in front, followed by ..." To replace it? ";
- echo strlen ($text) <= $len? $text: (substr ($text, 0, $len). chr (0). " ....");
Copy Code |