PHP substr intercept string, Chinese garbled problem solved

Source: Internet
Author: User
In the development of PHP programs, the interception of strings is often performed, such as the output of the list of information, the title should not be too long, when printing the article summary, but also to perform a series of string interception operations. When encountering these requirements, we often think of using the substr () method to achieve, substr () for the whole English string interception is more appropriate

But the string as long as the occurrence of Chinese characters, it is possible to cause PHP substr English garbled, because the Chinese UTF-8 encoding, each kanji accounted for 3 bytes, and GB2312 accounted for 2 bytes, English accounted for 1 bytes, the interception of bits inaccurate, substr () abruptly a Chinese character "saw" into two halves, The character that causes the break will be followed by the following. Pull over together to do a word, so appeared php substr Chinese garbled.


SUBSTR---get some strings

Syntax: String substr (string string, int start [, int length])
Description
SUBSTR () returns a portion of a string, specified by the parameter start and length.
If start is a positive number, the returned string will start at the start of the string, starting with the first character.
Example:

 
  

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

 
  

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.
Example:

 
  

For English no problem, we test a Chinese

Example:

 
  

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 to solve this problem:


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 string encoding, but the general server did not open Php_mbstring.dll, need to php.ini in the Php_mbstring.dll to open.

Example:

 
  


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.

Example:

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

The above describes the PHP substr intercept string, the Chinese garbled problem solved, including the content, I hope that the PHP tutorial interested friends helpful.

  • 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.