PHPsubstr intercepts Chinese garbled characters. Several character truncation functions are provided in php, including substr, mb_substr, and mb_strcut functions. some php beginners use substr to intercept Chinese characters, the results show that Chinese characters contain garbled characters. several character truncation functions are provided in php, including substr, mb_substr, and mb_strcut functions. some php beginners use substr to intercept Chinese characters, the result shows that there will be garbled characters in Chinese. we can use mb_substr to solve the garbled characters.
The description on the document page uses the substr function to intercept 220 characters, but the last Chinese character is always garbled and the length obtained is incorrect.
The Magic Google method may be found because substr (string, start, length) truncates Chinese characters in the form of characters, leading to garbled characters.
Solution:
Use the mb_substr method in the PHP Extension Library.
Note:
1. make sure that you have the php_mbstring.dll file in Windows/system32. if not, just export it to Windows/system32 from the extensions directory of your Php installation directory.
2. find php. ini in the windows directory to open the editor, search for mbstring. dll, and find
; Extension = php_mbstring.dll: remove the; number from the front, so that the mb_substr function can take effect.
Method definition:
String mb_substr (string str, int start [, int length [, string encoding])
Note: At the end of using mb_substr ()/mb_strcut, add another parameter to set the encoding of the string,
For example:
| The code is as follows: |
|
Echo mb_substr ('garbled Chinese characters will appear! ', 0, 7, 'utf-8 ′); |
Another example is:
| The code is as follows: |
|
$ Description = mb_substr (strip_tags ($ post-> post_content), 0,220, 'utf-8 ′); |
Mb_strcut function
The mb_strcut function can also intercept the string length. the following example shows the differences:
| The code is as follows: |
|
$ Str = 'so that my string will not contain garbled characters ^_^ '; Echo "mb_substr:". mb_substr ($ str, 0, 7, 'utf-8 '); // Result: Echo" "; Echo "mb_strcut:". mb_strcut ($ str, 0, 6, 'utf-8 '); // Result: ?> |
From the above example, we can see that mb_substr is used to split characters by words, while mb_strcut is used to split characters by bytes, but it does not produce half a character.
Substr () function Chinese normal substr () function can obtain the specified length of the string, but in case of Chinese, garbled characters may be generated at the end of the new string, the following function converts a string that exceeds the length of $ len to "and remove garbled characters.
Usage: $ new = getsubstring ($ old, 20 );
| The code is as follows: |
|
Function getsubstring ($ str, $ len) { For ($ I = 0; $ I <$ len; $ I ++) { If ($ I> = 0 AND $ I <$ len) { If (ord (substr ($ str, $ I, 1)> 0xa1) $ Result_str. = substr ($ str, $ I, 2 ); Else $ Result_str. = substr ($ str, $ I, 1 ); } If (ord (substr ($ str, $ I, 1)> 0xa1) $ I ++; } If (strlen ($ str) <= $ len) Return $ result_str; Else Return $ result_str ."..."; }
|
...