PHP Judgment String length strlen () and Mb_strlen () function

Source: Internet
Author: User
Tags ord
Strlen ()

PHP strlen () function

Definition and usage

The strlen () function returns the length of the string.

Grammar

Strlen (String)

Parameter: string
Description: Required. Specifies the string to check.

The code is as follows

 
  ‘; Echo Mb_strlen ($str, ' UTF8 '); Output//14//6?>

Results analysis: When strlen is calculated, the Chinese character that treats a UTF8 is 3 lengths, so the length of "1 characters" is 3*4+2=14
When the Mb_strlen is calculated, the selected inner code is UTF8, and a Chinese character is calculated as the length, so the "1 characters" length is 6


Mb_strlen () function

It is important to note that Mb_strlen is not a PHP core function and needs to ensure that Php_mbstring.dll is loaded in the php.ini before use, that is, to ensure

The "Extension=php_mbstring.dll" line exists and is not commented out, otherwise there is an issue with undefined functions.

The code is as follows

 
  

The strlen ($STR) value of the "Chinese a character 1 character" is 14,mb_strlen ($STR) value is 6, then you can calculate the placeholder for the "Chinese-a-word 1-character" is 10.

Explain the difference between the two

The code is as follows


 
  ';//14echo mb_strlen ($str, ' UTF8 '). '
';//6echo mb_strlen ($str, ' GBK '). '
';//8echo mb_strlen ($str, ' gb2312 '). '
';//10?>


Results analysis: In the strlen calculation, the treatment of a UTF8 Chinese characters is 3 length, so the "English a 1 characters" length is 3*4+2=14, in Mb_strlen

When calculating, the selected inner code is UTF8, then a Chinese character is calculated as the length, so the "1 character" is 6.


Although the above function can be a simple solution to some of the mixed problems in English and Chinese, but can not be used in real practice, let me give you friends to introduce other better

Method.

PHP to get the Chinese and English mixed string length implementation code is as follows, 1 english = 1 bits, 2 english = 1 bits, can be modified by themselves

The code is as follows

/*** PHP Gets the string in English mixed length * @param $str String String * @param $ $charset String encoding * @return return length, 1 Chinese = 1 bit, 2 english = 1 bit */function strlength ( $STR, $charset = ' utf-8 ') {if ($charset = = ' Utf-8 ') $str = Iconv (' utf-8 ', ' gb2312 ', $str); $num = strlen ($str); $cnNum = 0;for ($ i=0; $i < $num; $i + +) {if (Ord (substr ($str, $i +1,1)) >127) {$cnNum + +; $i + +;}} $enNum = $num-($cnNum *), $number = ($enNum/2) + $cnNum; return ceil ($number);} Test output length is 15$STR1 = ' test test testing Test testing test test test test '; $str 2 = ' aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa '; $str 3 = ' AA Test AA Test AA Test AA Test aaaaaa '; echo Strlength ($str 1, ' gb2312 '), Echo strlength ($str 2, ' gb2312 '), Echo strlength ($str 3, ' gb2312 ');


Intercepting String functions

UTF8 encoding, in UTF8, a Chinese character takes up 3 bytes

The code is as follows

function Msubstr ($str, $start, $len) {$tmpstr = ""; $strlen = $start + $len; for ($i = 0; $i < $strlen; $i + +) {  if (Ord (substr ($str, $i, 1)) > 127) {   $tmpstr. =substr ($str, $i, 3);   $i +=2;  } else   $tmpstr. = substr ($str, $i, 1);} return $tmpstr;} echo msubstr ("123 Days to Public 中文版", 0,10);


GB2312 encoding, in gb2312, a Chinese character takes up 2 bytes

The code is as follows

 
  0xa0) {           $tmpstr. = substr ($str, $i, 2);           $i + +;       } else           $tmpstr. = substr ($str, $i, 1);     }   return $tmpstr; }  ?>


Functions with good coding compatibility

The code is as follows

function Cc_msubstr ($str, $start =0, $length, $charset = "Utf-8", $suffix =true) {if (Function_exists ("Mb_substr"))  Return Mb_substr ($str, $start, $length, $charset); ElseIf (function_exists (' iconv_substr ')) {  return iconv_substr ($str, $start, $length, $charset);} $re [' Utf-8 ']   = "/[/x01-/x7f]| [/XC2-/XDF] [/x80-/xbf]| [/xe0-/xef] [/X80-/XBF] {2}| [/xf0-/xff] [/X80-/XBF] {3}/"; $re [' gb2312 '] = "/[/x01-/x7f]| [/xb0-/xf7] [/xa0-/xfe]/]; $re [' GBK ']   = "/[/x01-/x7f]|[ /x81-/xfe][/x40-/xfe]/"; $re [' Big5 ']   = "/[/x01-/x7f]|[ /X81-/XFE] ([/x40-/x7e]|/xa1-/xfe])/"; Preg_match_all ($re [$charset], $STR, $match); $slice = Join ("", Array_slice ($match [0], $start, $length)); if ($suffix) return $slice. " ..."; return $slice;}
  • 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.