Php judge string length strlen () and mb_strlen () functions

Source: Internet
Author: User
The strlen () PHPstrlen () function defines and uses the strlen () function to return the length of the string. Syntax strlen (string) parameter: string description: required. Specifies the string to be checked. The code is as follows... strlen ()

PHP strlen () function

Definition and usage

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

Syntax

Strlen (string)

Parameter: string
Description: required. Specifies the string to be checked.

The code is as follows:

 '; Echo mb_strlen ($ str, 'utf8'); // output result // 14 // 6?>

Result analysis: during strlen calculation, the Chinese character of UTF8 is 3 characters in length, so the length of "Chinese character a 1 character" is 3*4 + 2 = 14
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 "Chinese a character 1 character" is 6.


Mb_strlen () function

Note that mb_strlen is not a PHP core function. before using it, make sure that php_mbstring.dll is loaded in php. ini to ensure that

The line "extension = php_mbstring.dll" exists and is not commented out. Otherwise, the number of undefined functions may occur.

The code is as follows:

 

The strlen ($ str) value of "Chinese character a 1 character" is 14, and the mb_strlen ($ str) value is 6, the placeholder value of "Chinese character a 1 character" is 10.

Explain the differences between the two

The code is as follows:


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


Result analysis: during strlen calculation, the Chinese character of UTF8 is 3 characters in length, so the length of "Chinese character a 1 character" is 3*4 + 2 = 14, in mb_strlen

During calculation, if the selected inner code is UTF8, a Chinese character will be considered as the length of 1, so the length of "Chinese a character 1" is 6.


Although the above function can solve some mixed Chinese and English problems, it cannot be used in real time. next I will introduce other good functions to you.

Method.

The PHP code for obtaining the length of a mix of Chinese and English strings is as follows: 1 Chinese = 1 bit, 2 English = 1 bit, you can modify it yourself

The code is as follows:

/*** PHP obtains a string with a mix of Chinese and English characters * @ param $ str string * @ param $ charset string encoding * @ return returns the length. 1 Chinese character = 1 character, 2 English = 1 digit */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 + 127)>) {$ cnNum ++; $ I ++ ;}$ enNum = $ num-($ cnNum * 2); $ number = ($ enNum/2) + $ cnNum; return ceil ($ number) ;}// the length of the test output is 15 $ str1 = 'test test test'; $ str2 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa '; $ str3 = 'AA test aa test aaaaa'; echo strLength ($ str1, 'gb2312'); echo strLength ($ str2, 'gb2312 '); echo strLength ($ str3, 'gb2312 ');


Truncates string functions.

UTF8 encoding. in UTF8, a Chinese character occupies three 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 (" english );


GB2312 encoding. in gb2312, a Chinese character occupies 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 encoding 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.