The example in this article describes how to simply count the number of Chinese characters in PHP. Share it with everyone for your reference, the details are as follows:
The previous company was doing foreign trade and all the products were in English, so when counting the length, we used the strlen function, and there was no error. But now when we are counting Chinese, there is an error. Now we will do a record test.
<?php
echo strlen("你好ABC") . "";
# 输出 9
echo mb_strlen("你好ABC", 'UTF-8') . "";
# 输出 5
echo mb_strwidth("你好ABC") . "";
#输出 7
?>
From the above test, we can see:
strlen counts Chinese characters into 3 bytes
mb_strlen counts as 1 byte regardless of Chinese or English.
mb_strwidth counts Chinese as 2 bytes
Therefore, the mb_strlen function is used when calculating length statistics.