Home > Backend Development > PHP Tutorial > How to count the number of Chinese characters in php

How to count the number of Chinese characters in php

怪我咯
Release: 2023-03-11 22:06:01
Original
5671 people have browsed it

How to correctly count the number of Chinese characters in PHP? This is a problem that has troubled me for a long time. There are many functions in PHP that can calculate the length of string. For example, in the following example, three of

strlen
Copy after login
Copy after login
mb_strlen
Copy after login
Copy after login
mb_strwidth
Copy after login
Copy after login
Copy after login

are used respectively. Function to test the length of the statistical string to see how many bytes Chinese characters are calculated:

[code]echo strlen("你好ABC") . "";
# 输出 9
echo mb_strlen("你好ABC", 'UTF-8') . "";
# 输出 5
echo mb_strwidth("你好ABC") . "";
#输出 7
Copy after login

From the above test, we can see:

strlen
Copy after login
Copy after login

convert Chinese characters Counted as 3 bytes,

mb_strlen
Copy after login
Copy after login

regardless of Chinese or English, counted as 1 bytes, and

mb_strwidth
Copy after login
Copy after login
Copy after login

counted as Chinese Into 2 bytes, so

mb_strwidth
Copy after login
Copy after login
Copy after login

is what we want: 2 bytes for Chinese, 1 byte for English.
It is also recommended to use

mb_strimwidth
Copy after login

to intercept strings, which is also calculated according to the 2 bytes in Chinese and 1 byte in English, and if the number of words exceeds the interception requirements, This function can also automatically add '...' at the end.

[code]mb_strimwidth($post_excerpt,0,240,'...','utf-8');
Copy after login

Note that adding the ‘utf-8’ encoding parameter at the end can avoid the problem of garbled Chinese interception.

The above is the detailed content of How to count the number of Chinese characters in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template