PHP using substr () to intercept the string occurrence of Chinese garbled problem how to do _php instance

Source: Internet
Author: User

In PHP program development, often carry out string interception operations, such as the output Information list, the title should not be too long, print article summary, also perform a series of string interception operations. When encountering these requirements, we often think of using the substr () method to achieve, substr () to the whole English string interception is more appropriate.

But if the string appears in Chinese characters, it is possible to cause PHP substr Chinese garbled, because Chinese UTF-8 code, each Chinese character occupies 3 bytes, while the GB2312 occupies 2 bytes, English occupies 1 bytes, intercept the number of digits inaccurate, substr () abruptly to a Chinese character "saw" in two halves, The resulting disconnected characters will be followed by the ... Pull over to do a word together, so there are PHP substr Chinese garbled.

SUBSTR---Get a partial string

Syntax: String substr (string string, int start [, int length])

Description

SUBSTR () returns a part of a string that is specified by the parameter start and length.

If start is a positive number, the returned string will start with the first character of string.

Example:

<?php
$rest = substr ("abcdef", 1);//returns "Bcdef"
$rest = substr ("abcdef", 1, 3);//Returns "BCD"
? & Gt

If start is a negative number, the returned string will start at the end of string.

Example:

<?php
$rest = substr ("abcdef",-1);//returns "F"
$rest = substr ("ABCdef",-2);//returns "EF"
$rest = substr ("ABCdef",-3, 1); Returns "D"
?>

If there is a given parameter length and is a positive number, the returned string returns the length character from start.
If there is a given parameter length and is a negative number, the returned string will end at the length of the string at the end.

Example:

<?php
$rest = substr ("abcdef", 1,-1);//returns "BCDE"
?>

No problem with English, we test a Chinese

<?php
$rest = substr ("Chinese", 1,-1);//returns "FDSAFSDA" is garbled
?>

This kind of intercept character's result, certainly is not we want the result, this kind of occurrence PHP substr Chinese garbled situation, may cause the program not to run normally. There are two main ways to solve the problem:

First, the use of Mbstring expansion Library MB_SUBSTR () interception will not appear garbled.

You can use the Mb_substr ()/mb_strcut () function, the MB_SUBSTR ()/mb_strcut () is similar to the substr (), but at the end of Mb_substr ()/mb_strcut to add one more argument, To set the string encoding, but the general server did not open Php_mbstring.dll, need to php.ini in the Php_mbstring.dll open.

<?php
  echo mb_substr ("php Chinese character encode", 0,4, "Utf-8");
? >

If the last encoding parameter is not specified, it will be three bytes in Chinese, which is the characteristic of the Utf-8 encoding, and if added to the Utf-8 character set, it is intercepted in a single word.

Use the time to pay attention to the PHP file encoding, and Web page display when the code. Using this mb_substr method to know the encoding of strings in advance, if you do not know the encoding, you need to judge, Mbstring Library also provides mb_check_encoding to verify string encoding, but not perfect.

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

Substr sometimes cut 1/3 Chinese or half Chinese, will display garbled, relatively speaking mb_substr more suitable for us to use. But sometimes mb_substr seem less useful. For example, I want to display a small picture of the brief information, 5 Chinese just right, more than 5 to intercept the first 4 plus "...", so that the handling of Chinese is no problem, but the processing of English or digital, so the interception is too short.

Second, write the Intercept function, but the efficiency is not as high as the Mbstring expansion library. The following is a function of intercepting UTF-8 encoded strings within the 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, returns the string itself
  }
  ElseIf ($ Length <//If the interception length is a negative number
  {
    $length = $strlength + $length;//Then the interception is equal to the string length minus the interception length if
    ($length <)
    {
      $length = $strlength;//If the absolute value of the interception length is greater than the length of the string itself, the length of 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;
}

All right, this article is all about this, I believe it will bring you harvest, the above code is also very simple, there is no understanding of the place welcome to my message, I will give you the first time to answer.

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.