Php's simple method of counting Chinese characters

Source: Internet
Author: User
This article mainly introduces php's method for counting Chinese characters, including how to use strlen, mb_strlen, mb_strwidth, and other functions and related precautions, for more information about how to calculate the number of Chinese characters in php, see the example in this article. We will share this with you for your reference. The details are as follows:

The previous companies used English for foreign trade. Therefore, the length of statistics was measured using the strlen function, which has never been wrong. but now this error occurs when the Chinese language is counted, now let's test the record.

 

From the test above, we can see that:

Strlen converts Chinese characters into three bytes

Mb_strlen is counted as 1 byte in both Chinese and English.

Mb_strwidth converts Chinese into 2 bytes.

So we use the mb_strlen function for length statistics.

I hope this article will help you with PHP programming.

For more articles about the php method for counting the number of Chinese characters, refer to the PHP Chinese website!

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.