This article mainly introduces the method of counting the length of Chinese strings using PHP custom functions. It summarizes and analyzes PHP's operating skills related to Chinese judgment, encoding and operation in the form of examples. Friends in need can refer to the following
Chinese characters are calculated as 2 characters. English characters are calculated as 1
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Another: PHP determines the character length: Chinese, English, numbers.
There are many ways to do this. Record a simple one.
1 |
|
The disadvantage is that you need to install the mb library.
However, there are still some problems to be solved.
GB code encoding rules are as follows: each Chinese character consists of two bytes, the first byte ranges from 0XA1-0XFE, a total of 96 types. The range of the second byte is 0XA1-0XFE respectively, a total of 96 types. A total of 96 * 96 = 8836 Chinese characters can be defined using these two bytes. There are actually 6763 Chinese characters in total.
BIG5 code encoding rules are as follows: each Chinese character consists of two bytes, the first byte ranges from 0X81-0XFE, a total of 126 types. The range of the second byte is 0X40-0X7E, 0XA1-0XFE, a total of 157 types. In other words, a total of 126 * 157 = 19782 Chinese characters can be defined using these two bytes. Some of these Chinese characters are commonly used by us, such as Yi and D. These characters are called commonly used characters, and their BIG5 codes range from 0XA440 to 0XC671, a total of 5401 characters. Less commonly used characters, such as "tan" and "diao", are called less commonly used characters, ranging from 0XC940 to 0XF9FE, a total of 7652 characters, and the rest are some special characters.
A safer way.
1 2 3 4 5 6 7 8 9 |
|
Finally, the following is correct and universal!
Code:
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 |
|
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHPA simple way to determine whether a string contains another string_php skills
##
The above is the detailed content of Detailed explanation of how to use PHP custom function to count the length of Chinese strings. For more information, please follow other related articles on the PHP Chinese website!