Home > Backend Development > PHP Problem > PHP string learning how to return the length of Chinese characters or mixed characters

PHP string learning how to return the length of Chinese characters or mixed characters

青灯夜游
Release: 2023-03-11 22:58:01
Original
2158 people have browsed it

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 &#39;字符串 “&#39;.$str.&#39;” 的长度为:&#39;.strlen($str).&#39;<br>&#39;;
?>
Copy after login

The output result is:

PHP string learning how to return the length of Chinese characters or mixed characters

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 &#39;字符串 “&#39;.$str.&#39;” 的长度为:&#39;.mb_strlen($str,&#39;utf-8&#39;).&#39;<br>&#39;;
?>
Copy after login

The output result is:

PHP string learning how to return the length of Chinese characters or mixed characters

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!

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