Solving the problem of Chinese garbled characters in PHP intercept string

Source: Internet
Author: User
In the previous article we gave you the implementation of the PHP intercept string, and PHP interception of the use of Chinese strings, then we in the interception of Chinese strings, a lot of time will appear garbled problem, then we will introduce to you today PHP intercept string in Chinese garbled problem resolution!

PHP using SUBSTR to intercept strings in Chinese garbled problem with MB_SUBSTR
Instance:

MB_SUBSTR (' Intercept Chinese garbled problem test ', 0,5, ' utf-8 ');

Grammar:

String substr (string string, int start [, int length]) $rest = substr ("abcdef", 1); Returns "Bcdef" $rest = substr ("abcdef", 1, 3); Returns "BCD"

If start is a negative number, the returned string will start with the first word ending in string.

$rest = substr ("abcdef",-1); Returns "F" $rest = substr ("ABCdef",-2); Returns "EF" $rest = substr ("ABCdef",-3, 1); Returns "D"

If a parameter is given length and is a positive number, the returned string returns the length of the character from start.

If a parameter is given length and is a negative number, the returned string ends at the length of the number of characters ending in string.

$rest = substr ("abcdef", 1,-1); Returns "BCDE"

For English no problem, we test a Chinese

$rest = substr ("Chinese", 1,-1); Returns "FDSAFSDA" is garbled.

The result of this interception character is certainly not the result that we want, this kind of occurrence PHP substr Chinese garbled situation, may cause the program not to run normally.
There are two main ways of solving

First, the use of mbstring Extension Library Mb_substr () interception will not appear garbled.
can be used Mb_substr ()/mb_strcut () This function, Mb_substr ()/mb_strcut () with the use of substr () similar, only in Mb_substr ()/mb_strcut to add one more parameter, To set the encoding of the string,
But the general server did not open Php_mbstring.dll, need to php.ini in the Php_mbstring.dll to open.

echo mb_substr ("php Chinese characters encode", 0,4, "utf-8");

If the last encoding parameter is not specified, it will be three bytes to a Chinese, this is the Utf-8 encoding features, if added utf-8 character set description, so, is in a word to intercept.
Use the time to pay attention to the PHP file encoding, and page display when the encoding. Using this mb_substr method to know the encoding of the string beforehand, if you do not know the encoding, you need to judge, Mbstring Library also provides mb_check_encoding to verify the string encoding, but not perfect.

PHP comes with several string interception functions, which are commonly used in substr and MB_SUBSTR. The former in the processing of Chinese, GBK is 2 units of length, UTF is 3 units of length, the latter is specified after encoding, a Chinese is 1 length units.

Substr sometimes cut 1/3 Chinese or half of Chinese, will show garbled, relatively speaking mb_substr more suitable for us to use. But sometimes mb_substr doesn't look so good. For example, I want to display a small picture of the brief information, 5 Chinese exactly, more than 5 to intercept the top 4 Plus "...", so the Chinese is no problem, but to deal with English or numbers, so the interception is too short.

Second, write the interception function, but the efficiency is not as high as the Mbstring expansion library. The following is a function that intercepts UTF-8 encoded strings within Ecshop.

function Sub_str ($str, $length =, $append = True) {  $str = trim ($STR);  $strlength = strlen ($STR);  if ($length = = | | $length >= $strlength)  {    return $str;//intercept length equal to or greater than or equal to the length of this string, return the string itself  }  ElseIf ($ Length <)//If the intercept is negative  {    $length = $strlength + $length;//Then The intercept length is equal to the length of the string minus the intercept length    if ($length <)    { c11/> $length = $strlength;//If the absolute value of the intercept length is greater than the length of the string itself, the length of the string itself is truncated    }  if (function_exists (' Mb_substr ') )  {    $newstr = Mb_substr ($str,, $length, Ec_charset);  }  ElseIf (function_exists (' iconv_substr '))  {    $newstr = Iconv_substr ($str, $length, Ec_charset);  }  else  {    //$newstr = Trim_right (substr ($str, $length));    $newstr = substr ($str,, $length);  }  if ($append && $str! = $newstr)  {    $newstr. = ' ... ';  }  return $NEWSTR;}

Summarize:

This article through two examples for you to introduce the PHP interception string appeared in Chinese garbled solution, I believe that the same small partners in the emergence of the same problem, can be easily solved!

Related recommendations:

PHP intercepts Chinese strings and gets examples of Chinese string words


Introduction to how PHP intercepts strings

PHP Intercept string function substr () function instance usage detailed

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.