PHP uses substr () to intercept strings. what should I do if Chinese characters are garbled? _ PHP Tutorial

Source: Internet
Author: User
PHP uses substr () to intercept strings. what should I do if Chinese characters are garbled ,. In PHP, how to use substr () to intercept strings with Chinese garbled characters? in PHP program development, string truncation operations, such as output information list, are often executed, the title is not suitable for the use of substr () to intercept strings in PHP. what should I do if Chinese characters are garbled,

In PHP program development, string truncation is often performed. for example, when the output information list is used, the title should not be too long. when printing the article abstract, a series of string truncation operations should also be performed. When encountering these requirements, we often think of using the substr () method for implementation. substr () is more suitable for intercepting all English strings.

However, as long as the character string appears Chinese characters, it may cause PHP substr Chinese garbled characters, because of the Chinese UTF-8 encoding, each Chinese character occupies 3 bytes, while GB2312 occupies 2 bytes, English occupies 1 byte, the number of truncated digits is not accurate. substr () roughly splits a Chinese character into two halves, resulting in the disconnection of the character following .. the PHP substr is garbled in Chinese.

Substr --- obtain some strings

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

Note:

Substr () returns a string that is specified by the start and length parameters.

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

Example:

<?php$rest = substr ("abcdef", 1); // returns "bcdef"$rest = substr ("abcdef", 1, 3); // returns "bcd"?>

If start is a negative number, the returned string starts with the start character at the end of the string.

Example:

<?php$rest = substr ("abcdef", -1); // returns "f"$rest = substr ("abcdef", -2); // returns "ef"$rest = substr ("abcdef", -3, 1); // returns "d"?>

If the length parameter is given and it is a positive number, the returned string will return length characters from start.
If the length parameter is given and it is a negative number, the returned string ends with the nth length character ending with the string.

Example:

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

There is no problem with English. let's test a Chinese character.

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

The result of intercepting characters is definitely not what we want. in this case, PHP substr Chinese garbled characters may cause the program to fail to run normally. There are two solutions:

1. use mb_substr () to intercept the extended library without garbled characters.

You can use the mb_substr ()/mb_strcut () function. the usage of mb_substr ()/mb_strcut () is similar to that of substr (), but only in mb_substr () /mb_strcut adds another parameter at the end to set the encoding of the string, but php_mbstring.dll is not opened on the general server. ini opens php_mbstring.dll.

<? Php echo mb_substr ("php Chinese character encode", "UTF-8");?>

If the last encoding parameter is not specified, three bytes are used as a Chinese character. this is the feature of UTF-8 encoding. if the UTF-8 character set is added, it is captured in the unit of a word.

Pay attention to the PHP file encoding and the encoding when displaying the webpage. To use this mb_substr method, you need to know the encoding of the string in advance. if you do not know the encoding, you need to judge it. the mbstring library also provides mb_check_encoding to check the encoding of the string, but it is not complete yet.

PHP comes with several string truncation functions, including substr and mb_substr. When the former is processing Chinese characters, GBK is two length units, and UTF is three length units. after encoding is specified, a Chinese character is one length unit.

Substr sometimes intercepts 1/3 Chinese or half Chinese characters, and garbled characters are displayed. mb_substr is more suitable for use. However, sometimes mb_substr is not so easy to use. For example, I want to display the brief information of a small image. five Chinese characters are exactly the same. if there are more than five Chinese characters, I will take the first four and add "...", It is okay to process Chinese in this way, but it is too short to process English or numbers.

2. write the truncation function by yourself, but the efficiency is not as high as using the mbstring Extension Library. The following is the function of intercepting the string in the UTF-8 encoding in ecshop.

Function sub_str ($ str, $ length =, $ append = true) {$ str = trim ($ str); $ strlength = strlen ($ str ); if ($ length = | $ length >=$ strlength) {return $ str; // The truncation length is equal to or greater than or equal to the length of the string, returned string} elseif ($ length <) // if the truncation length is negative {$ length = $ strlength + $ length; // the truncation length is equal to the string length minus the truncation length if ($ length <) {$ length = $ strlength; // if the absolute value of the truncation length is greater than the length of the string, extract the length of the string} 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 it. I believe it will bring you some gains. the above code is also very simple. if you have any questions, please leave a message. I will reply to you immediately.

What should I do if Chinese garbled characters occur during the capture () string? in PHP program development, the string truncation operation is often executed. for example, when the information list is output, the title is not suitable...

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.