In PHP if I want to use substr () to intercept the string in English is no problem, if included in Chinese or English will be tragic, but we also do not cut we can use other methods to solve.
PHP interception of Chinese characters garbled, this is the recent discovery of things, previously I wrote an article about the automatic meta-information generated
, the article about the use of PHP to intercept the article before the number of words as Description method, but there has been IE6 unable to load CSS phenomenon, here
Make a supplement.
First of all to clarify such a problem, the reason is IE6 occasionally can not load the phenomenon of CSS, because the file is garbled, resulting in
The link to the loaded CSS of the polygon cannot be parsed correctly by IE6. So you see a plain HTML page, no CSS, naked!
Clear the problem, the rest of the problem is good to solve, is to prevent garbled, since the function provided by Wango has been garbled, 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 have problems if you include Chinese characters.
MB_SUBSTR () The use of this function is similar to that of substr (), except that one more parameter is added at the end to set the encoding of the string.
By the way, you should understand why I have improved the Million-GE method ~ ~
Here are a few more advanced approaches
Example 1
The code is as follows |
Copy Code |
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 occupies three bytes:
public static function Chinesesubstr ($str, $start, $len) {//$str refers to a string, $start refers to the string's
Starting position, $len refers to the string length
$strlen = $start + $len; Stores the total length of a string, from the starting position of the string to the character, with $strlen
The total length of the string
The code is as follows |
Copy Code |
for ($i = $start; $i < $strlen;) { if (Ord (substr ($str, $i, 1)) > 0xa0) {//If the ASCII ordinal of the first byte in a string A value greater than 0xa0 indicates the kanji $tmpstr. = substr ($str, $i, 3); Each time the three-bit word is taken out assigned to the variable $tmpstr, i.e. In a Chinese character $i = $i +3; Variable self-add 3 } else{ $tmpstr. = substr ($str, $i, 1); If it is not a Chinese character, each time you take out a single word assigned Variable $tmpstr $i + +; } } return $tmpstr; return string } |
http://www.bkjia.com/PHPjc/633064.html www.bkjia.com true http://www.bkjia.com/PHPjc/633064.html techarticle in PHP If I want to use substr () to intercept the string in English is no problem, if included in Chinese or English will be tragic, but we also do not cut we can use other methods to solve. P ...