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

php chr() ord()中文截取乱码问题解决方法

WBOY
Release: 2016-06-13 12:27:09
Original
1415 people have browsed it

复制代码代码如下:


$lenth = 19;
$str = "怎么将新闻的很长的标题只显示前面一些字,后面用.....来代替?";
echo strlen($str)?>


复制代码代码如下:



/*
@ 另一种方法,使用ord()函数:
@ 适用于 gb2312 编码:
*/
$str = "怎么将新闻的很长的标题只显示前面一些字,后面用.....来代替?";
function gb2312_substr($str, $limit) {
$restr ='';
for($i=0;$i$restr .= ord($str[$i])>127 ? $str[$i].$str[++$i] : $str[$i];
}
return $restr;
}
/*
@ 以下仅适用于 utf-8 编码;
*/
function utf8_substr($str, $limit) {
$restr = '';
for($i=0;$i$restr .= ord($str[$i])>127 ? $str[$i].$str[++$i].$str[++$i] : $str[$i];
}
return $restr;
}
//解释下上面第一个:chr(0)不是null,null是什么都没有,而chr(0)的值是0。表示成16进制是0x00,表示成二进制是00000000虽然chr(0)不会显示出什么,但是他是一个字符。虽然chr(0)不会显示出什么,但是他是一个字符。当汉字被截断时,根据编码规则他总是要把后边的其他字符拉过来一起作为汉字解释,这就是出现乱码的原因。
?>

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
    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!