Explain the functions of mb_strlen and strlen in php

Source: Internet
Author: User
Tags php tutorial strlen
<? Php Tutorial
// During the test, the file encoding method is utf8.
$ Str = 'Chinese character a 1 ';
Echo strlen ($ str). '<br>'; // 14
Echo mb_strlen ($ str, 'utf8'). '<br>'; // 6
Echo mb_strlen ($ str, 'gbk'). '<br>'; // 8
Echo mb_strlen ($ str, 'gb2312'). '<br>'; // 10
?>

Result analysis: during strlen calculation, the Chinese character of utf8 is 3 characters in length, so the length of "Chinese character a 1 character" is 3*4 + 2 = 14, when mb_strlen is calculated, if the selected inner code is utf8, a Chinese character will be calculated as the length of 1. Therefore, the length of "Chinese a character 1 character" is 6.
The two functions can be used to calculate the placeholder value of a string in both Chinese and English (the placeholder value of a Chinese character is 2 and that of an English character is 1)
Echo (strlen ($ str) + mb_strlen ($ str, 'utf8')/2;
For example, the strlen ($ str) value of "Chinese character a 1 character" is 14, and the mb_strlen ($ str) value is 6, the placeholder value of "Chinese character a 1 character" is 10.
Echo mb_internal_encoding ();
The built-in string length function strlen in php cannot properly process Chinese strings. It only obtains the number of bytes occupied by strings. For gb2312 Chinese encoding, strlen gets twice the number of Chinese characters, while for UTF-8 encoded Chinese, it is three times the difference (in UTF-8 encoding, A Chinese character occupies 3 bytes ).
Using the mb_strlen function can better solve this problem. The usage of mb_strlen is similar to that of strlen, except that it has a second optional parameter for specifying character encoding. For example, to obtain the length of the UTF-8 string $ str, you can use mb_strlen ($ str, 'utf-8 '). If the second parameter is omitted, the internal code of php is used. The internal encoding can be obtained through the mb_internal_encoding () function. It should be noted that mb_strlen is not a php core function. Before using it, make sure that. the "php_mbstring.dll" line is loaded in ini to ensure that the "extension = php_mbstring.dll" line exists and is not commented out. Otherwise, the number of undefined functions may occur.

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.