PHP comes with several string interception functions, among which substr and mb_substr are commonly used. When the former processes Chinese, GBK is 2 length units and UTF is 3 length units. After the latter specifies encoding, one Chinese character is 1 length unit.
mb_substr Usage
1 |
|
mb_substr Perform a multibyte-safe substr() operation based on the number of characters. Calculated from the starting position of str. The first character is at position 0. The position of the second character is 1, and so on:
str is the intercepted parent string.
start starting position.
length The maximum length of the returned string. If omitted, it will be cut to the end of str.
encoding parameter is character encoding. If omitted, the internal character encoding is used.
Then we can use the following code to complete this problem.
1 |
|
gb2312 is the Chinese encoding format.
mb_substr handles mixed Chinese and English strings
substr sometimes cuts off 1/3 Chinese or half Chinese and displays garbled characters. Relatively speaking, mb_substr is more suitable for us to use. But sometimes mb_substr doesn't seem so useful. For example, if I want to display the brief information of a small picture, 5 Chinese characters are just right. If there are more than 5 characters, just intercept the first 4 and add "...". This is no problem when processing Chinese, but when processing English or numbers, this interception will be too short. . This problem can be solved by using the following function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|