PHP截断标题且兼容utf8和gb2312编码
发布人:shili8
发布时间:2022-12-10 12:37
阅读次数:19
复制代码 代码如下:
<?php if(strlen($r[title])>45){$str=utf8substr($r[title],0,15)."...";}else{$str=$r[title];}echo $str; ?>
//截取utf8字符串
function utf8substr($str, $from, $len)
{
return preg_replace('#^(?:[\x00-\x7f]|[\xc0-\xff][\x80-\xbf]+){0,'.$from.'}'.
'((?:[\x00-\x7f]|[\xc0-\xff][\x80-\xbf]+){0,'.$len.'}).*#s',
'$1',$str);
}