php chr() ord()中文截取乱码问题解决方法
复制代码 代码如下:
$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)不会显示出什么,但是他是一个字符。当汉字被截断时,根据编码规则他总是要把后边的其他字符拉过来一起作为汉字解释,这就是出现乱码的原因。
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
