A simple method to count the number of Chinese characters in PHP

高洛峰
Release: 2023-03-03 18:14:01
Original
1396 people have browsed it

The example in this article describes how to simply count the number of Chinese characters in PHP. I share it with you for your reference. The details are as follows:

The previous company was engaged in foreign trade and all the products used were in English, so when counting the length, I used the strlen function, and there was no error. But now when counting Chinese, this function Something went wrong, now do a record test

<?php
echo strlen("你好ABC") . "";
# 输出 9
echo mb_strlen("你好ABC", &#39;UTF-8&#39;) . "";
# 输出 5
echo mb_strwidth("你好ABC") . "";
#输出 7
?>
Copy after login

From the above test, we can see:

strlen counts Chinese characters as 3 bytes

mb_strlen regardless of Chinese or English, it counts as 1 byte

mb_strwidth counts Chinese as 2 bytes

So when calculating the length, use the mb_strlen function

I hope this article will be helpful to everyone in PHP programming.

For more articles related to how to simply count the number of Chinese characters in PHP, please pay attention to 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!