Home > Backend Development > PHP Tutorial > PHP string length calculation - introduction to the use of strlen() function

PHP string length calculation - introduction to the use of strlen() function

高洛峰
Release: 2023-03-04 15:30:02
Original
1933 people have browsed it

strlen() function and mb_strlen() function

In PHP, the function strlen() returns the length of the string. The function prototype is as follows:

int strlen(string string_input);
Copy after login

The parameter string_input is the string to be processed.

strlen() function returns the length of bytes occupied by the string. An English letter, number, and various symbols all occupy one byte, and their lengths are all 1. A noon character occupies two bytes, so the length of a noon character is 2. For example

<?php 
echo strlen("www.php.cn"); 
echo strlen("PHP中文网"); 
?>
Copy after login

The running result of "echo strlen("www.php.cn");": 15

The running result of "echo strlen("PHP Chinese Network");": 15

There is a question here. Doesn’t a Chinese character occupy 2 bytes? "Sanzhi Development Network" clearly has five Chinese characters, so how could the result be 15?

The reason is here: when calculating strlen(), for a UTF-8 Chinese character, it will be treated as having a length of 3. When there is a mix of Chinese and English, how to accurately calculate the length of the string? Here, another function mb_strlen() must be introduced. The usage of the mb_strlen() function is almost the same as strlen(), except that there is an additional parameter that specifies the character set encoding. The function prototype is:

int mb_strlen(string string_input, string encode);
Copy after login

PHP’s built-in string length function strlen cannot correctly handle Chinese strings. It only gets the number of bytes occupied by the string. For GB2312 Chinese encoding, the value obtained by strlen is twice the number of Chinese characters, while for UTF-8 encoded Chinese, the difference is three times (under UTF-8 encoding, one Chinese character occupies 3 bytes). Therefore, the following code can accurately calculate the length of Chinese strings:

<?php 
$str = "三知sunchis开发网"; 
echo strlen($str)."<br>"; //结果:22 
echo mb_strlen($str,"UTF8")."<br>"; //结果:12 
$strlen = (strlen($str)+mb_strlen($str,"UTF8"))/2; 
echo $strlen; //结果:17 
?>
Copy after login

Principle analysis:

strlen() When calculating, the length of Chinese characters treated in UTF-8 is 3, so " The length of "Sanzhi Sunchis Development Network" is 5×3+7×1=22
When calculating mb_strlen, if the internal code is selected as UTF8, a Chinese character will be calculated as a length of 1, so "Sanzhi "sunchis Development Network" has a length of 5×1+7×1=12

The rest is a pure mathematical problem, so I won’t go into details here...

Note: For mb_strlen($ str,'UTF-8'), if the second parameter is omitted, PHP's internal encoding will be used. The internal encoding can be obtained through the mb_internal_encoding() function. It should be noted that mb_strlen is not a core function of PHP. Before using it, you need to make sure that php_mbstring.dll is loaded in php.ini, that is, make sure that the line "extension=php_mbstring.dll" exists and is not commented out, otherwise it will be undefined. function problem.

For more PHP string length calculation - introduction to the use of strlen() function, please pay attention to the PHP Chinese website for related articles!

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