PHP substr interception of Chinese garbled problem Solution _php Tutorial

Source: Internet
Author: User
In PHP to provide us with a few character interception functions, including substr,mb_substr,mb_strcut function, some of us PHP beginners will use substr to intercept Chinese, the results found that Chinese will have garbled, garbled we can use the mb_substr to solve.

The description of the article page is the use of the SUBSTR function to intercept 220 characters, but the last character is always garbled, and the truncated length is not correct.

Through the magic of Google to find a way, probably because substr (string,start,length), will be characters in the form of truncation, and cause garbled

Solution:

Use the Mb_substr method in the PHP extension library.

Attention

1. Make sure you have php_mbstring.dll this file under your windows/system32, and do not copy it into Windows/system32 from your PHP installation directory extensions.
2. Find php.ini in Windows directory Open edit, search Mbstring.dll, find
Extension=php_mbstring.dll the front of the number, so that the MB_SUBSTR function can take effect.


Method definition:

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

Note: At the end of the Mb_substr ()/mb_strcut, add one more parameter to set the encoding of the string,

For example:

The code is as follows Copy Code

Echo Mb_substr (' The original characters will appear garbled! ', 0, 7, ' utf-8′);

Again such as:

The code is as follows Copy Code

$description = Mb_substr (Strip_tags ($post->post_content), 0,220, ' utf-8′ ');

Mb_strcut function

Mb_strcut function can also intercept the length of the string, the following example specifically to see where the difference:

The code is as follows Copy Code

$str = ' so that my string will not have garbled ^_^ ';

echo "MB_SUBSTR:". Mb_substr ($str, 0, 7, ' utf-8 ');
Result: so that my word
echo "
";

echo "Mb_strcut:". Mb_strcut ($str, 0, 6, ' utf-8 ');
Result: this
?>

As can be seen from the above example, Mb_substr is to divide characters by word, while mb_strcut splits characters by Byte, but does not produce a half-character phenomenon.

The substr () function Chinese version of the normal substr () function can get a string of a specified length substring, but encountered in Chinese can be garbled at the end of a new string, the following function will be more than $len length of the string converted to "..." end, and removed garbled.
Usage: $new = getsubstring ($old, 20);

The code is as follows Copy Code
function getsubstring ($STR, $len)
{
for ($i = 0; $i < $len; $i + +)
{
if ($i >=0 and $i < $len)
{
if (Ord (substr ($str, $i, 1)) > 0xa1)
$result _str.=substr ($str, $i, 2);
Else
$result _str.=substr ($str, $i, 1);
}
if (Ord (substr ($str, $i, 1)) > 0xa1)
$i + +;
}
if (strlen ($STR) <= $len)
return $result _str;
Else
return $result _str. " ...";
}

http://www.bkjia.com/PHPjc/633190.html www.bkjia.com true http://www.bkjia.com/PHPjc/633190.html techarticle in PHP to provide us with a few character interception functions, including the Substr,mb_substr,mb_strcut function, some of our PHP beginners will use substr to intercept Chinese, the results found that Chinese will be garbled ...

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