$len){ $title ="/> $len){ $title =">

substr的有关问题

WBOY
Release: 2016-06-13 10:21:08
Original
748 people have browsed it

substr的问题
在输出文章标题的时候,由于框架宽度限制,我只能最多容许10个中文长度的标题出现
超出10个的话则以9个中文+ .. 表示,比如"一二三四五六七八九.."
我的函数体很简单,

function doTitleLen($title, $len){
if(strlen($title) > $len){
$title = substr($title, 0, $len-2).'..';

}
return $title;
}

假设len为10
然后传来的文章标题是
"一二三四五六七八九十1"
这样会导致最后一个字为乱码,因为substr里-2了,这是按照1个中文长度去减的
不知怎么解决这个BUG
strlen得出的是字节数,如何计算字数?
并且得到了字数后,有什么函数可以按照字符个数去截取,而不是字节数去截取的呢?

------解决方案--------------------
用mb_strlen()计算字符长度。
用mb_substr()截取。

注意指定编码。

Related labels:
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 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!