


PHP string learning how to return the length of Chinese characters or mixed characters
In the previous article "How does PHP change a substring of a certain length into a * sign", we introduced string replacement, a method of replacing a substring of a certain length into a * sign, this This time we continue to understand strings and introduce the method of calculating the length of Chinese strings or the length of mixed Chinese and English strings.
How to calculate the length of Chinese string? Regarding this problem, our first reaction may be to use the strlen() function, but the strlen() function is good at processing all-English strings, but it does not work when it comes to mixed Chinese and English strings or pure Chinese strings. Let’s take a look at the following example:
<?php header("Content-type:text/html;charset=utf-8"); //设置字符编码为utf-8 $str = "欢迎来到PHP中文网!"; echo '字符串 “'.$str.'” 的长度为:'.strlen($str).'<br>'; ?>
The output result is:
It can be seen that the strlen() function cannot be accurate due to different rules for processing characters. Returns the length of mixed Chinese and English or pure Chinese strings. So why does it return 27?
That’s because the character encoding we use is utf-8, and a Chinese character occupies three bytes under UTF-8 encoding.
strlen() function In ASCII code, one Chinese character occupies two bytes of space; in UTF-8 encoding, one Chinese character (including traditional Chinese) is equal to three bytes; in Unicode Under encoding, one Chinese character (including traditional Chinese) is equal to two bytes.
In the above example, one of the strings "Welcome to the PHP Chinese website!
" contains 8 Chinese characters and 3 English characters, then 8 *3 3=27
bytes, then the string length is 27.
But such a string length is not what we want. How to deal with Chinese characters and accurately calculate the length of the mixed Chinese and English or pure Chinese string we want?
PHP provides a function to solve this problem, that is the mb_strlen() function.
Still the previous example, this time using the mb_strlen() function to calculate the string length:
<?php header("Content-type:text/html;charset=utf-8"); $str = "欢迎来到PHP中文网!"; echo '字符串 “'.$str.'” 的长度为:'.mb_strlen($str,'utf-8').'<br>'; ?>
The output result is:
It can be seen that in the string $str
, there are 11 Chinese and English exclamation marks, and the output result is also 11. By the way, I found the right person~
mb_strlen() function, whether it is Chinese characters or English, numbers, decimal points, underlines and spaces, only takes up one byte.
Let’s take a brief look at the mb_strlen() function.
mb_strlen($string,$encoding)
The function can return the corresponding number of characters by setting the character encoding; this function accepts a required parameter $string (the string that needs to be detected) and a The omitted parameter $encoding
(character encoding), if omitted, the internal character encoding is used.
We can handle the length problem of Chinese strings well by setting the corresponding $encoding
parameters.
Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial
Finally, I recommend reading a classic course "PHP String Processing (Jade Girl Heart Sutra Edition)", it's free~ come and learn !
The above is the detailed content of PHP string learning how to return the length of Chinese characters or mixed characters. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
