A solution to the garbled character of substr in PHP function

Source: Internet
Author: User
Tags ord
This article introduces, in the use of PHP string interception function substr, the interception of Chinese characters in the case of garbled solution. Please refer to the friends who are in need.

PHP string intercept function substr:

String substr (string $string, int $start [, int $length]) returns a string of length from start position

substr function in the interception of characters, is truncated in bytes, the Chinese characters in the GB2312 encoding is 2 bytes, utf-8 encoding is 3 bytes, so if the truncation of a specified length of the character string, then the returned results will appear garbled.

The following two solutions are available for your reference.

1, use the MB_SUBSTR function instead of string mb_substr (string $str, int $start [, int $length [, String $encoding]]) similar to the substr () function, except that the count is counted by the number of characters, Guaranteed character Security using the MB_SUBSTR () function guarantees that no garbled characters are present. Cons: The length statistic becomes the count of characters rather than the number of bytes counted. For display, the same length of Chinese results and English results will show a large difference in display length.

2, the self-built function enhances the SUBSTR function to calculate according to 2 length units, causes the Chinese and English mixed use environment the final display length of the string interception result to be close; 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 , it has good versatility.

The complete code is as follows (using the Strtolower function):

      $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;                $noc + = 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]. $string [$i + 1];                $i + +;                } else {$wordscut. = $string [$i];    }}} $string = $wordscut; } return Trim ($string);} Example Echo getstr ("01234567", 1). '
'; 0echo getstr ("1,234,567", 2). '
'; 0echo getstr ("1,234,567", 3). '
'; 01 Echo getstr ("01234567", 4). '
'; 01 Echo getstr ("01234567", 5). '
'; 012 Echo getstr ("01 a two B 34567", 1). '
'; 0echo Getstr ("1 a two B 34567", 2). '
'; 0echo Getstr ("1 a two B 34567", 3). '
'; 01 Echo getstr ("01 a two B 34567", 4). '
'; 01 Aecho getstr ("01 a two B 34567", 5). '
'; 01 a//This function is modified by the GETSTR () function in Uchome 1.5.?>
  • 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.