How to Count Chinese words in PHP

Source: Internet
Author: User

How to correctly count Chinese words in PHP? This is a problem that has plagued me for a long time, there are many functions in PHP that can calculate the length of a string, such as the following example, respectively, using the

Strlen
Mb_strlen
Mb_strwidth

This three function to test the length of the statistical string, to see how to count Chinese into several bytes:

[Code]echo strlen ("Hello abc"). ""; # Output 9echo mb_strlen ("Hello abc", ' UTF-8 '). "; # Output 5echo mb_strwidth ("Hello abc"). ""; #输出 7

From the above test, we can see:

Strlen

The Chinese characters are counted as 3 bytes,

Mb_strlen

Whether Chinese or English, it counts 1 bytes, and

Mb_strwidth

The Chinese is counted as 2 bytes, so

Mb_strwidth

is what we want: Chinese 2 bytes, English 1 bytes .
It is also recommended to use the same intercept string

Mb_strimwidth

, is also according to the Chinese 2 byte, the English 1 byte method calculates after, and if the word number exceeds the interception request, this function can also automatically add ' ... ' in the last side.

[Code]mb_strimwidth ($post _excerpt,0,240, ' ... ', ' utf-8 ');

Note that the last addition of the ' Utf-8 ' encoding parameter, can avoid the problem of Chinese truncation garbled.

Related Article

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.