The Chinese and English mix counts, intercepts, does not use the custom function, but uses PHP's MB extension, uses the original ecology PHP function to handle the string interception easily. First, the following functions are used to intercept strings. Mb_strwidth ($STR, $encoding) returns the width of the string $str the string to be evaluated $encoding the encoding to use, such as UTF8, Gbkmb_strimwidth ($str, $start, $width, $tail, $encoding) intercept strings by width $str the string to intercept $start from, the default is 0$width to intercept the width $tail appended to the string that is behind the intercept string, commonly used is ... $encoding the encoding to use Example:
- /**
- * UTF8 encoding format
- * 1 Chinese takes 3 bytes
- * We want 1 Chinese to occupy 2 bytes,
- * Because the width of the 2 English letters occupy the position equivalent to 1 Chinese
- */
- Test string
- $STR = ' aaaa ah ah aaaa ah ah aaa ';
- echo strlen ($STR); Use only strlen output to 25 bytes
- You must specify the encoding, or you will use PHP's internal code mb_internal_encoding () to view the inner code
- Use the Mb_strwidth output string with a width of 20 using UTF8 encoding
- Echo mb_strwidth ($str, ' UTF8 ');
- Only a width greater than 10 is intercepted.
- if (Mb_strwidth ($str, ' UTF8 ') >10) {
- Set here to intercept from 0, take 10 append ..., use UTF8 encoding
- Note the additional ... will also be counted within the length of the
- $str = mb_strimwidth ($str, 0, ' ... ', ' UTF8 ');
- }
- Finally output aaaa ah ... 4 A, 4, 1, 2, 3, 3, 4+2+3=9.
- is not very simple ah, some people say why is 9 not 10 of them?
- Because just "ah" behind or "ah", Chinese 2, 9+2=11 beyond the set, so remove one is 9
- Echo $str;
Copy CodeOther string intercept functions: Mb_strlen ($STR, $encoding) returns the length of the string $str the encoding to use for the string $encoding to be evaluated Mb_substr ($str, $start, $length, $encoding) intercept the string $str the string to intercept $start from where to intercept $length intercept how long $encoding used the encoding actually these 2 functions and strlen () , substr () is like, the only difference is that you can set the encoding. An example of the above two string intercept functions.
- /**
- * UTF8 encoding format
- * 1 Chinese takes 3 bytes
- */
- $STR = ' aa12 ah aa ';
- echo strlen ($STR); Direct output Length of 9
- The output length is 7, why is it 7?
- Note that after the code is set, either Chinese or English, each length is 1
- A a 1 2 ah a A
- 1+1+1+1+1+1+1 = 7
- Is it just a 7-character?
- Echo Mb_strlen ($str, ' UTF8 ');
- Same mb_substr, too.
- I only want 5 characters now
- Echo mb_substr ($str, 0, 5, ' UTF8 '); Output aa12.
Copy CodeMB Extension library also has a lot of practical functions, this is not introduced here, you can refer to the following PHP manual for the relevant content. |