How to solve PHP substr garbled problem

Source: Internet
Author: User
Tags php language
PHP substr is a functional function that is developed based on the PHP language that can return a part of a string. Sometimes we use PHP substr will appear some garbled problem, then how to solve it?

String substr (string $string, int $start [, int $length])

Returns a string that starts at length from the start position

The SUBSTR function is truncated by byte when the character is intercepted, the Chinese characters are 2 bytes in the GB2312 encoding, and the Utf-8 encoding is 3 bytes, so if the Chinese character is clipped when the string of the specified length is intercepted, the returned result will appear garbled.

Check it out, the solution boils down to two categories:

1. Use the MB_SUBSTR () function instead

String Mb_substr (string $str, int $start [, int $length [, String $encoding]])

Similar to the substr () function, only counting is counted by the number of characters, guaranteeing character security

Using the MB_SUBSTR () function guarantees that no garbled characters are present, but the disadvantage is that the length statistic becomes the count of chars rather than the number of bytes. For display, the same length of Chinese results and English results will show a large difference in display length.

2. Self-built function enhances substr function

Here is a function to better solve the problem of substr encountering Chinese characters. The Chinese characters are calculated according to 2 units of length, which makes the final display length of the string interception results close to each other in Chinese and English mixed environment. Discard the last incomplete character, ensure that the display is not garbled, and compatible with Chinese characters commonly used utf-8 encoding and GB2312 encoding, has a good versatility.

View Source code

  function Getstr ($string, $length, $encoding = ' utf-8 ') {$string = Trim ($string);      if ($length && strlen ($string) > $length) {//truncate character $wordscut = ';      if (Strtolower ($encoding) = = ' Utf-8 ') {//utf8 encoded $n = 0;     $tn = 0;     $noc = 0;     while ($n < strlen ($string)) {$t = Ord ($string [$n]); if ($t = = 9 | | $t = = 10 | | (<= $t && $t <= 126))     {$tn = 1;      $n + +;     $noc + +;     } elseif (194 <= $t && $t <= 223) {$tn = 2;    $n + = 2;      } elseif (224 <= $t && $t < 239) {$tn = 3;      $n + = 3;     $noc + = 2;     } elseif (<= $t && $t <= 247) {$tn = 4;     $n + = 4;     $noc + = 2;    } elseif (248 <= $t && $t <= 251) {$tn = 5;       $n + = 5;     $noc + = 2;       } elseif ($t = = 252 | | $t = = 253) {$tn = 6;       $n + = 6;      $noc + = 2;    } else {$n + +;     } if ($noc >= $length) {break; }} if ($noc > $Length) {$n-= $tn;     } $wordscut = substr ($string, 0, $n); } else {for ($i = 0; $i < $length-1; $i + +) {if (Ord ($string [$i]) > 127) {$wordscut. = $string [$i]. $st    ring[$i + 1];     $i + +;      } else {$wordscut. = $string [$i];     }}} $string = $wordscut;     } return Trim ($string); }//Example echo getstr ("01234567", 1). '  <br/> '; 0 echo getstr ("1,234,567", 2). '  <br/> '; 0 echo getstr ("1,234,567", 3). '  <br/> '; 01 Echo getstr ("01234567", 4). '  <br/> '; 01 Echo getstr ("01234567", 5). '  <br/> '; 012 Echo getstr ("01 a two B 34567", 1). '    <br/> '; 0 echo Getstr ("1 a two B 34567", 2). '    <br/> '; 0 echo Getstr ("1 a two B 34567", 3). '    <br/> '; 01 Echo getstr ("01 a two B 34567", 4). '    <br/> '; 01 a echo getstr ("01 a two B 34567", 5). '    <br/> '; 01 A

This function is modified by the GETSTR () function in Uchome 1.5.

The above is how to solve the PHP substr garbled problem of the scheme, before we also shared a series of articles on PHP substr function, there is a need to pay attention to topic.alibabacloud.com.

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.