PHP string length calculation-strlen () Function Usage Introduction

Source: Internet
Author: User
Strlen () function and Mb_strlen () function

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

int strlen (string string_input);

The parameter string_input is the string to be processed.

The strlen () function returns the byte length of a string, with an English letter, a number, and a variety of symbols representing one byte, each of which has a length of 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 ("topic.alibabacloud.com");?>

"Echo strlen (" www.php.cn "); Running results: 15

"Echo strlen (" topic.alibabacloud.com "); Running results: 15

Here's a question, does a Chinese character account for 2 bytes? "Three-knowledge Development network", is clearly five characters, the results of the operation is 15?

The reason is here: strlen () calculation, for a UTF-8 of Chinese characters, it will be treated as a length of. How to accurately calculate the length of a string when there is a mixture of Chinese and English? Here, you have to introduce another function, Mb_strlen (). The use of the Mb_strlen () function is almost identical to that of strlen (), except that it has more than one parameter that specifies the character set encoding. The function prototypes are:

int Mb_strlen (string string_input, string encode);

PHP built-in string length function strlen cannot handle Chinese strings correctly, it only gets the number of bytes that the string occupies. For the Chinese encoding of GB2312, strlen get the value is twice times the number of Chinese characters, and for UTF-8 encoded in Chinese, is 3 times times the difference (in UTF-8 encoding, a Chinese character accounted for 3 bytes). Therefore, the following code can accurately calculate the length of the Chinese string:

<?php $str = "Three-sunchis development network"; echo strlen ($STR). " <br> "; Results: Echo mb_strlen ($str, "UTF8"). " <br> "; Results: $strlen = (strlen ($str) +mb_strlen ($str, "UTF8"))/2; Echo $strlen; Results:?>

Principle Analysis:

Strlen (), the length of the UTF-8 is 3, so the length of the "three-known sunchis development net" is 5x3+7x1=22
In the Mb_strlen calculation, the selected inner code is UTF8, will be a Chinese character as the length of the calculation, so "three-known sunchis development network" length of 5x1+7x1=12

The rest is purely mathematical problems, this is not wordy ...

Note: For Mb_strlen ($str, ' UTF-8 '), if you omit the second argument, PHP's internal encoding is used. The internal code can be obtained through the mb_internal_encoding () function. It is important to note that Mb_strlen is not a PHP core function and needs to be sure that the Php_mbstring.dll is loaded in the php.ini before use, that is, to ensure that the "Extension=php_mbstring.dll" line is present and not commented out. Otherwise, there is an issue with undefined functions.

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.