Home > php教程 > php手册 > body text

php截取字符串函数,支持中文汉字无乱码

WBOY
Release: 2016-06-13 11:36:36
Original
961 people have browsed it

用php substr函数截取字符串一部分,但是字符串中含有汉字时,结果就出现了乱码,导致网页标签混乱,显示不完整。

找到函数一个,分享一下。

Copy to Clipboard引用的内容:[www.bkjia.com] function msubstr($str, $start, $len) {
$tmpstr = “”;
$strlen = $start + $len;
for($i = 0; $i if(ord(substr($str, $i, 1)) > 0xa0) {
$tmpstr .= substr($str, $i, 2);
$i++;
} else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}

* 可以避免乱码的截取汉字
* 参数 $str 为字符串,$start 为开始字符,$len 结束字符
* 返回截取后的字符

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!