Home > Article > Backend Development > Regarding the solution to the problem of garbled Chinese characters intercepted by substr() in php
This article mainly introduces the PHP substr() function. Here is a code example to illustrate the problem of garbled characters when intercepting Chinese strings. Friends in need can refer to it
If I want to use it in PHP There is no problem if substr() intercepts a string that is all in English. If it includes Chinese or English, it will be a tragedy, but 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 was about using php to intercept the first few words of the article as description method, but IE6 cannot load CSS. Here is 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, causing the subsequent link to load CSS to not be correctly parsed by IE6. So I saw a pure HTML page, no CSS, naked! After clarifying the problem, the remaining problem can be easily solved, which is to prevent garbled characters. Since the function provided by Wange has garbled characters, I found a new 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.
mb_substr() The usage of this 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 the Wange method~~
Here are some more advanced processing methods.
Example 1
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; }
Example 2
The string is encoded as 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 starting position of the string, $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 total length of the string
The code is as follows
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; // 返回字符串 }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
php Detailed graphic explanation of accessing oracle stored procedures
phpImplements the method of obtaining the current url address
PHP implements the method of receiving the binary stream and converting it into an image
The above is the detailed content of Regarding the solution to the problem of garbled Chinese characters intercepted by substr() in php. For more information, please follow other related articles on the PHP Chinese website!