Question about GBK string truncation ~

Source: Internet
Author: User
Question about GBK string truncation ~~~~ This is a function for intercepting GBK strings: PHPcodefunctiongb_substr ($ str, $ len) {$ count = 0; for ($ I = 0) question about intercepting GBK strings ~~~~
This is a function that intercepts GBK strings:
PHP code
  function  gb_substr( $str ,  $len ){              $count  = 0;              for ( $i =0;  $i < strlen ( $str );  $i ++){                  if ( $count  ==  $len )  break ;                  if (preg_match( "/[\x80-\xff]/" ,  substr ( $str ,  $i , 1))){        ++ $i ;          }                  ++ $count ;                      }              return   substr ( $str , 0,  $i );        }


My question is, is each GBK and GB2312 not necessarily two bytes? Is the length of X 2 enough to be intercepted?
For example, I want to take 3 characters: 3*2 = 6, that is, I want to take 6 characters.

Is that true?

------ Solution --------------------
Using functions of the mb series, GBK pairs are non-ASCII characters, and ASCII is still 1 byte.
------ Solution --------------------
No, because it may contain ascii numbers or characters.

Discussion
This is a function that intercepts GBK strings:
PHP code

Function gb_substr ($ str, $ len ){
$ Count = 0;
For ($ I = 0; $ I <strlen ($ str); $ I ++ ){
If ($ count = $ len) break ......

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.