Several character truncation functions are provided for us in php, including substr, mb_substr, and mb_strcut functions. some php beginners use substr to intercept Chinese characters and find Chinese characters contain garbled characters, we can use mb_substr to solve the garbled characters.
Several character truncation functions are provided for us in php, including substr, mb_substr, and mb_strcut functions. some php beginners use substr to intercept Chinese characters and find Chinese characters contain garbled characters, 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, resulting in 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. in the windows directory, find php. ini and open the editor. search for mbstring. dll. find; extension = php_mbstring.dll and remove the number; so that the mb_substr function will 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, echo mb_substr ('the Chinese character will contain garbled characters! ', 0, 7, 'utf-8 ′);
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:
-
- $ 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 ."...";
- }