1. Intercept GB2312 Chinese string
The code is as follows |
Copy Code |
<?php Intercepting Chinese strings function Mysubstr ($str, $start, $len) { $tmpstr = ""; $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; } ?> |
2. Intercepts the UTF8 encoded multibyte strings
The code is as follows |
Copy Code |
<?php Intercepting UTF8 strings function Utf8substr ($str, $from, $len) { Return Preg_replace (' #^: [x00-x7f]|[ xc0-xff][x80-xbf]+) {0, '. $from. '} '. ' ((?: [x00-x7f]| [Xc0-xff] [x80-xbf]+) {0, '. $len. '}). * #s ', ' $1′, $STR); } ?> |
However, in the case of mixing English with Chinese characters, the following problems arise:
If there is such a string
The code is as follows |
Copy Code |
$str = "This is a string";
|
To intercept the first 10 characters of the string, use the
The code is as follows |
Copy Code |
if (strlen ($STR) >10) $str =substr ($STR, 10). " ...";
|
So, the echo $str output should be "This is a word ..."
So how to solve this problem, I would like to share one can support gb2312,gbk,big three kinds of coding.
Example 1
The code is as follows |
Copy Code |
$len = 19; $text = "How will the long title of the news show only a few words in front of, back with ..." To replace it? "; echo strlen ($text) <= $len? $text: (substr ($text, 0, $len). chr (0). " ...."); /****CHR (0) is not NULL Null is nothing, and the value of Chr (0) is 0. 16 is 0x00, which means binary is 00000000 Although Chr (0) will not show anything, but he is a character. When the Chinese characters are truncated, according to the coding rules he always vlasov the other characters in the back to explain the characters, this is the reason for the garbled. Values of 0x81 to 0xFF and 0x00 are always displayed as "null" According to this feature, in the substr after the result of a CHR (0), you can prevent the occurrence of garbled Note: Encode first byte second byte gb2312 0xa1-0xf7 0xa1-0xfe GBK 0x81-0xfe 0x81-0xfe 0x40-0x7e Big5 0xa1-0xf7 0x81-0xfe 0x40-0x7e Second, this is online search, support Utf-8 encoding, the original author unknown: *****/ function Substring_utf8 ($str, $start, $lenth) { $len = strlen ($STR); $r = Array (); $n = 0; $m = 0; for ($i = 0; $i < $len; $i + +) { $x = substr ($str, $i, 1); $a = Base_convert (ord ($x), 10, 2); $a = substr (' 00000000 '. $a,-8); if ($n < $start) { if (substr ($a, 0, 1) = 0) { }elseif (substr ($a, 0, 3) = 110) { $i + 1; }elseif (substr ($a, 0, 4) = 1110) { $i + 2; } $n + +; }else{ if (substr ($a, 0, 1) = 0) { $r [] = substr ($str, $i, 1); }elseif (substr ($a, 0, 3) = 110) { $r [] = substr ($str, $i, 2); $i + 1; }elseif (substr ($a, 0, 4) = 1110) { $r [] = substr ($str, $i, 3); $i + 2; }else{ $r [] = '; } if (+ + $m >= $lenth) { Break } } } return $r; }//End Substring_utf8; }//End String |
#由于此函数返回的是一个数组, so you want to match the join function to display the string:
#join (", Substring_utf8 ($str, $start, $lenth));
#在页面显示的时候还可以在此语句后面连一个 "..."