PHP Mb_strlen () Chinese-English mixed character intercept code _php tutorial

Source: Internet
Author: User
Note: If the fatal error:call to undefined function Mb_strlen appears with Mb_strlen, you can use info () to see if there is no load mbstring, if not, try to Php_ Mbstring.dll Copy to the%windows% directory

File Encoding Utf-8

$var = ' Chinese characters abc ';
Mb_strlen ($var, ' utf-8 '); Output 7 Chinese and English are one byte
Mb_strlen ($var); Output 15 Chinese to 3 bytes English takes one byte
Mb_strlen ($var, ' GBK '); Output 9 not normal

Take all Chinese

function Utf8substr ($str, $from, $len) {return preg_replace (' #^ (?: [x00-x7f]|[ xc0-xff][x80-xbf]+) {0, '. $from. '} '. ' (?: [x00-x7f]| [Xc0-xff] [x80-xbf]+) {0, '. $len. '}). * #s ', ' $ ', $str); }

Chinese and English mixed body intercept code

function Gb2312_strlen ($string) {$str _len = strlen ($string); $str _count = 0;for ($j = 0; $j < $str _len; $j + +) {   if (ord ($string {$j}) < 127)   {    $str _count + = 1;    Continue;   }   else   {    if (ord ($string {$j +1}) > 127)    {     $str _count + = 1;     $j + +;     Continue;    }    else    {     $str _count + = 1;     Continue;}}}   return $str _count;} $STR = "Open s D";  echo Gb2312_strlen ($STR);
  • 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.