Calculate Chinese string length in PHP, intercept Chinese string

Source: Internet
Author: User

In the development of PHP, because of our country's language environment problems, so we often need to deal with the Chinese. In PHP, we all know that there are special MB_SUBSTR and Mb_strlen functions that can intercept and calculate the length of Chinese, but since these functions are not the core functions of PHP, they are often not open. Of course, if you are using your own server, just open it in php.ini. If the virtual host is used, and the server does not have the function to open this aspect, it is necessary for us to write a point suitable for our national conditions of the function. Here are some of the functions that are quite handy. But you know, you have to use it in a utf-8 environment.
Header(' Content-type:text/html;charset=utf-8 ');/** * A function that can count the length of a Chinese String * @param $str the string to be computed for length * @param $type calculated length type, 0 (default) means one character in Chinese, 1 for a Chinese figure of two characters **/functionAbslength ($str){    if(Empty($str)){        return0; }    if(function_exists(' Mb_strlen ')){        returnMb_strlen ($str, ' Utf-8 '); }    Else {        Preg_match_all("/./u",$str,$ar); return Count($ar[0]); }}$str= ' We are all Chinese, ye!. ‘;$len= Abslength ($str);Var_dump($len);//return$len= Abslength ($str, ' 1 ');Echo' <br/> '.$len;//return /*Utf-8 code to intercept the Chinese string, parameters can refer to the SUBSTR function @param $str to intercept the string @param $start to intercept the beginning of the position, negative number for the reverse interception @param $end to be intercepted Length*/functionUTF8_SUBSTR ($str,$start=0) {    if(Empty($str)){        return false; }    if(function_exists(' Mb_substr ')){        if(Func_num_args() >= 3) {            $end=Func_get_arg(2); returnMB_SUBSTR ($str,$start,$end, ' Utf-8 '); }        Else{mb_internal_encoding ("UTF-8"); returnMB_SUBSTR ($str,$start); }            }    Else {        $null= ""; Preg_match_all("/./u",$str,$ar); if(Func_num_args() >= 3) {            $end=Func_get_arg(2); return Join($null,Array_slice($ar[0],$start,$end)); }        Else {            return Join($null,Array_slice($ar[0],$start)); }    }}$str 2= ' wo to intercept Zhongwen ';Echo' <br/> ';EchoUTF8_SUBSTR ($str 2, 0,-4);//return wo to intercept Zhon

Support Gb2312,gbk,utf-8,big5 Chinese interception method

/** Chinese intercept, support gb2312,gbk,utf-8,big5 * * @param string $STR the string to intercept * @param int $start intercept start position * @param int $lengt H Intercept Length * @param string $charset UTF-8|GB2312|GBK|BIG5 encoded * @param $suffix suffix*/  Public functionCSUBSTR ($str,$start=0,$length,$charset= "Utf-8",$suffix=true)   {      if(function_exists("Mb_substr"))      {          if(Mb_strlen ($str,$charset) <=$length)return $str; $slice= Mb_substr ($str,$start,$length,$charset); }      Else    {          $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); if(Count($match[0]) <=$length)return $str; $slice=Join("",Array_slice($match[0],$start,$length)); }      if($suffix)return $slice." ..."; return $slice; } 

Calculate Chinese string length in PHP, intercept Chinese string

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.