Analysis of the differences between PHP's built-in function strlen and mbstring's extension function mb_strlen

不言
Release: 2023-04-03 08:16:02
Original
1780 people have browsed it

This article mainly introduces the difference between the PHP built-in function strlen and the mbstring extension function mb_strlen. It has a certain reference value. Now I share it with you. Friends in need can refer to it

#EXAMPLE

$str_uncode = "简体中文Chinese(Simplified)";
//统计字符串长度
echo strlen($str_uncode).&#39;<br>&#39;;//结果:31
echo mb_strlen($str_uncode, &#39;UTF-8&#39;).&#39;<br>&#39;;结果:23
echo mb_strlen($str_uncode, &#39;GBK&#39;).&#39;<br>&#39;;结果:25
echo mb_strlen($str_uncode, &#39;GB2312&#39;).&#39;<br>&#39;;结果27
Copy after login

Result analysis:

strlen Count a Chinese character as 3 bytes (complex Chinese characters will be counted as 4 bytes)

mb_strlen 'UTF- 8' Encoding a Chinese character is calculated as one byte

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).

Using the mb_strlen function can better solve this problem. The usage of mb_strlen is similar to strlen, except that it has a second optional parameter to specify the character encoding. For example, to get the UTF-8 string $str length, you can use 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.

mb_strlen is not a PHP core function. 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 has not been commented out, otherwise an error message will appear. The problem of defining functions.

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to scan url dead links using PHP

How to use var_export in PHP

The above is the detailed content of Analysis of the differences between PHP's built-in function strlen and mbstring's extension function mb_strlen. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!