Home  >  Article  >  Backend Development  >  php substr() function intercepts garbled Chinese string_PHP tutorial

php substr() function intercepts garbled Chinese string_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:44:451282browse

In php, if I want to use substr() to intercept the string in English, it will be no problem. If it includes Chinese or English, it will be a tragedy, but everyone also Don't worry, we can use other methods to solve it.

When php intercepts Chinese strings, garbled characters appear. This is a recent discovery. I have previously written an article about automatically generating meta information

, that article is about using php to intercept the first few words of the article as the description method, but there is a phenomenon that IE6 cannot load CSS, here

Make a supplement.

First of all, we need to clarify this problem. The reason why IE6 occasionally fails to load CSS is because the file is garbled, resulting in

The link loading CSS above cannot be correctly parsed by IE6. So I saw a pure HTML page, no CSS, naked!

After clarifying the problem, the remaining problems can be easily solved, which is to prevent garbled characters. Since the function provided by Wange has garbled characters, just look for it again

I created a php function to solve this garbled code problem.

The

substr() function can split text, but problems often occur if the text to be split includes Chinese characters.

The usage of mb_substr() function is similar to substr(), except that one more parameter is added at the end to set the encoding of the string.

After reading this, you should understand the reason why I improved Wange’s method~~

Here are some more advanced processing methods

Example 1

Return $tmpstr;
The code is as follows
 代码如下 复制代码

function func_chgtitle($str,$len) { //$length我们允许字符串显示的最大长度    
    $tmpstr = "";
    $strlen = $len;
     for($i = 0; $i < $strlen; $i++) {
if(ord(substr($str, $i, 1)) > 0xa0) {
            $tmpstr .= substr($str, $i, 2);
            $i++;
         } else
            $tmpstr .= substr($str, $i, 1);
     }
     return $tmpstr;

}

Copy code

function func_chgtitle($str,$len) { //$length The maximum length we allow for string display 

$tmpstr = "";

$strlen = $len;

for($i = 0; $i < $strlen; $i++) {

If(ord(substr($str, $i, 1)) > 0xa0) {
                 $tmpstr .= substr($str, $i, 2);               $i++;

           } else

                 $tmpstr .= substr($str, $i, 1);
 代码如下 复制代码

        for($i = $start; $i < $strlen;) {
if (ord ( substr ( $str, $i, 1 ) ) > 0xa0) { // 如果字符串中首个字节的ASCII序数

值大于0xa0,则表示汉字
                $tmpstr .= substr ( $str, $i, 3 ); // 每次取出三位字符赋给变量$tmpstr,即等

于一个汉字
                $i=$i+3; // 变量自加3
            } else{
                $tmpstr .= substr ( $str, $i, 1 ); // 如果不是汉字,则每次取出一位字符赋给

变量$tmpstr
                $i++;
            }
        }
        return $tmpstr; // 返回字符串
    }

}
} Example 2 The string encoding is UTF-8, and one Chinese character occupies three bytes: public static function chinesesubstr($str, $start, $len) { // $str refers to the string, $start refers to the string Starting position, $len refers to the length of the string $strlen = $start + $len; // Use $strlen to store the total length of the string, that is, from the starting position of the string to the character The total length of the string
The code is as follows Copy code
for($i = $start; $i < $strlen;) {<🎜> If (ord ( substr ( $str, $i, 1 ) ) > 0xa0) { // If the ASCII ordinal number of the first byte in the string value greater than 0xa0 means Chinese character $ TMPSTR. = Substr ($ Str, $ i, 3); // Take the three characters each time to give the variable $ TMPSTR, that is, On a Chinese character $ I = $ i+3; // Variables from 3                } else{                      $tmpstr .= substr ( $str, $i, 1 ); // If it is not a Chinese character, take out one character at a time and assign it to Variable $tmpstr                    $i++;             } }            return $tmpstr; // Return string }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633064.htmlTechArticleIn php, if I want to use substr() to intercept a string in English, no problem, if it includes Chinese or It will be a tragedy in English, but don’t worry, we can use other methods to solve it. p...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn