In PHP if I want to use substr () to intercept the string in English, if the inclusion of Chinese or English will be tragic, but we do not cut we can use other ways to solve.
PHP interception of Chinese string garbled, this is the recent discovery of things, previously I have written an article on the automatic generation of meta information, that article on the use of PHP to intercept the article before how many words as a description method, but there are IE6 can not load the phenomenon of CSS, here to do a supplement.
First of all to clear such a problem, the reason will appear IE6 occasionally can not load the phenomenon of CSS, because the file appears garbled, resulting in the subsequent load CSS link can not be IE6 correct resolution. So I saw a plain HTML page, no CSS, naked! Clear the problem, the remaining problem is good to solve, is to prevent garbled, since the Wango provided by the function of garbled, it went back to find a PHP function to solve this garbled problem.
The substr () function splits the text, but the text you want to split can often encounter problems if you include Chinese characters.
The use of MB_SUBSTR () is similar to the substr (), except that a single argument is added at the end to set the string encoding.
The reason why I have improved the method of GE is to be understood by the large number here.
Here are a few more advanced approaches
Example 1
The code is as follows
function Func_chgtitle ($STR, $len) {//$length we allow the maximum length of the string display
$tmpstr = "";
$strlen = $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;
}
Example 2
The string is encoded as UTF-8 and a Chinese character takes up three bytes:
Public static function Chinesesubstr ($str, $start, $len) {//$str refers to a string, $start the starting position of the string, $len the length of the string
$strlen = $start + $len ; Stores the total length of a string in $strlen, from the start of the string to the total length of the string
The code is as follows
for ($i = $start; $i < $strlen;) {
if (ord ($str, $i, 1)) > 0xa0) {//If the first byte of the string has an ASCII ordinal
value greater than 0xa0, the character $tmpstr is represented
. = substr ($str , $i, 3); Every time you remove three-digit assignments to the variable $tmpstr, which is equal
to a Chinese character
$i = $i +3;//variable from 3
} else{
$tmpstr. = substr ($str, $i, 1);//If not kanji, each time Take out a bit of character assignments to the
variable $tmpstr
$i + +;
}
return $tmpstr; return string
}
Through this article hope that the PHP programming for everyone to deal with the same problem help!