[PHP] a simple and wonderful way to get the length of a Chinese string _ PHP Tutorial

Source: Internet
Author: User
[PHP] a simple and brilliant way to get the length of a Chinese string. When writing the form verification class of the framework tonight, you need to determine whether the length of a string is within the specified range. Naturally, you think of the strlen function in PHP. $ StrHelloworld !; Echostr needs to judge whether the length of a string is within the specified range when writing the form verification class of the framework tonight. Naturally, he thought of the strlen function in PHP.
$ Str = 'Hello world! ';
Echo strlen ($ str); // output 12
However, in PHP built-in functions, strlen and mb_strlen both calculate the length by calculating the number of bytes occupied by the string. the number of bytes occupied by Chinese characters varies with encoding. In GBK/GB2312, the Chinese character occupies 2 bytes, while in the UTF-8, the Chinese character occupies 3 bytes.
$ Str = 'Hello, World! ';
Echo strlen ($ str); // output 12 under GBK or GB2312, output 18 under UTF-8
While we often need to judge the length of the string is the number of characters, rather than the number of bytes occupied by the string, such as the PHP code under the UTF-8:
$ Name = 'Zhang Geng Chang ';
$ Len = strlen ($ name );
// Output FALSE because three Chinese characters in the UTF-8 account for 9 bytes
If ($ len> = 3 & $ len <= 8 ){
Echo 'true ';
} Else {
Echo 'false ';
}
So what convenient and practical methods can be used to obtain the length of a string containing Chinese characters? We can use regular expressions to calculate the number of Chinese characters, divided by 2 in GBK/GB2312 encoding, divided by 3 in UTF-8 encoding, and finally added the length of non-Chinese characters, however, this is not too troublesome. WordPress has a more beautiful code. the reference is as follows:
$ Str = 'Hello, World! ';
Preg_match_all ('/./us', $ str, $ match );
Echo count ($ match [0]); // output 9
The idea is to use a regular expression to divide a string into a single character and calculate the number of matched characters using count, which is what we want.


Bytes. $ Str = Hello world !; Echo str...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.