The method of using PHP function to calculate the length of Chinese and English strings, _php tutorial

Source: Internet
Author: User

The method of using PHP function to calculate the length of Chinese and English strings,


This paper describes the method of using PHP function to calculate the length of Chinese and English strings. Share to everyone for your reference. The implementation method is as follows:

In general, you know the English characters accounted for a byte, while the Chinese characters gbk two characters, utf8 three characters, many people think that the PHP string length is the strlen () function, in fact, it calculates the length of the byte rather than the length of the character, So how do you get the length of a character in a string? There are also Mb_strlen ().

The specific code is as follows:
Copy the Code code as follows: Echo $str = ' php dot-pass ';

echo strlen ($STR); 3*1+3*3=12
Echo Mb_strlen ($str, ' gb2312 '); 3*1+3*2=9
Echo Mb_strlen ($str, ' utf-8 '); 6
Damn, the MB series of functions is not the PHP core function, the default is not open, there is a super-simple method, through the regular string decomposition into individual characters, the number of characters is the length of the string, the code is as follows:
Copy the Code code as follows: <?php
function _strlen ($STR)
{
Preg_match_all ("/./us", $str, $matches);
Return count (current ($matches));
}

echo _strlen ("PHP dot-pass"); 6
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/909334.html www.bkjia.com true http://www.bkjia.com/PHPjc/909334.html techarticle using PHP function to calculate the length of Chinese and English strings, this paper describes the method of using PHP function to calculate the length of Chinese and English strings. Share to everyone for your reference. Concrete implementation of the party ...

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