Home  >  Article  >  Backend Development  >  PHP interception string function, supports Chinese characters without garbled characters_PHP tutorial

PHP interception string function, supports Chinese characters without garbled characters_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:54:00812browse

Use the php substr function to intercept part of the string, but when the string contains Chinese characters, the result will be garbled, resulting in confusing web page tags and incomplete display.

Find a function and share it.

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] function msubstr($str, $start, $len) {
$tmpstr = “”;
$strlen = $start + $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;
}

* Can avoid garbled interception of Chinese characters
* Parameter $str is a string, $start is the start character, $len is the end character
* Returns the intercepted characters

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364695.htmlTechArticleUse the php substr function to intercept part of the string, but when the string contains Chinese characters, the result will be garbled, resulting in The web page labels are confusing and the display is incomplete. Find a function and share it...
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