Differences in how strlen and Mb_strlen functions are used in PHP

Source: Internet
Author: User

(1) strlen get string length


(2) Mb_strlen
How to use: Int Mb_strlen (String $str [, String $encoding])
Returns the length of the given string. The encoding parameter is a character encoding. If omitted, the internal character encoding is used.

<?PHP//How to encode a file when testing if UTF8$str ='Chinese a character 1 characters'; echo strlen ($STR).'<br>';// -Echo Mb_strlen ($STR,'UTF8').'<br>';//6Echo Mb_strlen ($STR,'GBK').'<br>';//8Echo Mb_strlen ($STR,'gb2312').'<br>';//Ten?>

GBK encoded state A Chinese character is 2 bytes, UTF-8 encoded in the state of a Chinese character is 3 bytes

In strlen calculation, treats a UTF8 character is 3 length, so "Chinese a character 1 character" length is 3*4+2=14, in Mb_strlen calculation, the selected inner code is UTF8, will be a Chinese character as the length of the calculation, so "the word a 1 character" length is 6.

The two functions can be combined to calculate the number of placeholders for a mixed Chinese-English string (a placeholder for a Chinese character is 2, the English character is 1)

if ((strlen ($company _name) +mb_strlen ($company _name,"UTF8")/2 >$len  ) {    return109;}

Differences in how strlen and Mb_strlen functions are used in PHP

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.