The news section of a recently developed PHP website project requires a function to extract strings Based on the title width and add "... ", the first thought is to use the PHP mb_string function implementation, mb_strimwidth, mb_strwidth, and then found that if the title contains the" "symbol, PHP mb_strwidth considers this symbol as one width. I wonder if this is not a Chinese Double quotation mark. It should be a byte in width, and the length should be two widths, after the query "" unicode respectively u201C and u201D, not in the range of Chinese characters, and then query the unicode.org code table, found that u2000-u206F is the range of universal symbols, although the characters in this range are all in the form of wide characters, the mb _ function of PHP considers it to be 1 width, and there is no way but to rely on your own.
The following functions are implemented:
- Function truncString ($ str, $ length)
- {
- $ CountLen = 0;
- For ($ I = 0; $ I <mb_strlen ($ str); $ I ++)
- {
- $ CountLen + = amb_strwidth (mb_substr ($ str, $ I, 1 ));
- If ($ countLen> $ length)
- Return mb_substr ($ str, 0, $ I );
- }
- Return $ str;
- }
- Function amb_strwidth ($ str_width)
- {
- $ Count = 0;
- For ($ I = 0; $ I <mb_strlen ($ str_width); $ I ++)
- {
- // If (mb_substr ($ str_width, $ I, 1) = "xE2x80x9C" | mb_substr ($ str_width, $ I, 1) = 'xe2x80x9d ')
- // If characters in the u2000-u206F are encountered, the counter is added to 2
- If (preg_match ("/[x {2000}-x {206F}]/u", mb_substr ($ str_width, $ I, 1 )))
- $ Count + = 2;
- Else
- $ Count + = mb_strwidth (mb_substr ($ str_width, $ I, 1 ));
- }
- Return $ count;
- }
The above is the specific solution to the problem when using PHP mb_strwidth. I hope it will help you.