Home  >  Article  >  Backend Development  >  PHP截断标题且兼容utf8和gb2312编码_php技巧

PHP截断标题且兼容utf8和gb2312编码_php技巧

WBOY
WBOYOriginal
2016-05-17 08:55:101206browse
复制代码 代码如下:

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);
}
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