PHPstrlen () and mb

Source: Internet
Author: User
Strlen-get string Length description intstrlen (string $ string) returns the length of the given string. The length of the string parameter. If the return value is successful, the length of the string is returned. If the return value is null, the return value is 0. Mb_strlen-get the length description of the string mixedmb

Strlen-get string Length description int strlen (string $ string) returns the length of the given string. The length of the string parameter. If the return value is successful, the length of the string is returned. If the return value is null, the return value is 0. Mb_strlen-get the length description of the string mixed mb

Strlen-get the string length

Description

IntStrlen(String$string)

Returns the given string.string.

Parameters

stringThe string to be calculated.

Return Value

Returns a string.stringLength; ifstringIf it is null, 0 is returned.


Mb_strlen-get the string length

Description

MixedMb_strlen(String$str[, String$encoding= Mb_internal_encoding ()])

Returns the length of a string.

Parameters

strThe string to be checked.
encoding encodingThe parameter is character encoding. If omitted, internal character encoding is used.

Return Value

ReturnsencodingEncoded stringstrThe number of characters. Multi-byte characters are counted as 1.

IfencodingInvalidFALSE.

The above is the explanation of the two functions in the manual. At first glance, it seems that they are similar.

The following example describes the differences between the two.

// The file encoding method is UTF8 $ str = 'character a, string B, 2 degrees long '; echo strlen ($ str ).'
'; // 19 echo mb_strlen ($ str, 'utf-8 ').'
'; // 9

Let's look at the next example.

 '; // 14 echo mb_strlen ($ str, 'utf-8 ').'
'; // 9?>

Result Analysis: When the strlen character length is reached, the Chinese character of UTF8 is counted as three characters in length,

Therefore, the length of "word a character 1 string B length 2 degrees" is 3 + 1 + 3 + 1 + 3 + 1 + 3 + 1 + 3 = 19,

When mb_strlen is calculated, if the selected inner code is UTF8, a Chinese character is regarded as 1 in length. Therefore, the length of "character a, string B, length 2 degrees" is 9.

In gb2312, when strlen calculates the character length, a Chinese character is counted as two characters.

In addition, it should be noted that mb_strlen is not a PHP core function. before using it, make sure to load php_mbstring.dll in php. ini; otherwise, the number of undefined functions may occur.

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.