Php intercepts Chinese strings and garbled characters. This was recently discovered. I have previously written an article about automatic generation of meta information.
That article describes how many words are used as the description method before the interception by php, but IE6 cannot load CSS.
Make a supplement.
First of all, we need to clarify this problem. The reason why IE6 occasionally fails to load CSS is that the file is garbled.
Link loaded with CSS cannot be correctly parsed by IE6. Therefore, we can see a pure HTML page, without CSS, naked!
If the problem is identified, the remaining problem will be solved, that is, to prevent garbled code. Since the function provided by Wange has garbled code, you can find it again.
To solve this garbled problem.
The substr () function can split text, but if the text to be split contains Chinese characters, it will often encounter problems.
The usage of the function mb_substr () is similar to that of substr (), but an additional parameter must be added at the end to set the encoding of the string.
Here we should understand the reasons for improving the HiChina method ~~
Next we will introduce several more advanced solutions.
Example 1
The code is as follows: |
Copy code |
Function func_chgtitle ($ str, $ len) {// $ length the maximum length that can be displayed by a string $ 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
Character string encoded as UTF-8, a Chinese character occupies three bytes:
Public static function chinesesubstr ($ str, $ start, $ len) {// $ str indicates a string, and $ start indicates a string
Starting position. $ len indicates the string length.
$ Strlen = $ start + $ len; // use $ strlen to store the total length of the string, that is, from the starting position of the string to the character
Total length of a string
The code is as follows: |
Copy code |
For ($ I = $ start; $ I <$ strlen ;){ If (ord (substr ($ str, $ I, 1)> 0xa0) {// if the first byte of the string contains the ASCII ordinal number If the value is greater than 0xa0, it indicates Chinese characters. $ Tmpstr. = substr ($ str, $ I, 3); // each time a three-character is taken out and assigned to the variable $ tmpstr, that is, etc. In a Chinese character $ I = $ I + 3; // variable auto-Increment 3 } Else { $ Tmpstr. = substr ($ str, $ I, 1); // if it is not a Chinese character, a character is taken out and assigned Variable $ tmpstr $ I ++; } } Return $ tmpstr; // return a string } |