Home > Backend Development > PHP Tutorial > php中怎么对输出的文章限制字数,剩余的用“.”表示

php中怎么对输出的文章限制字数,剩余的用“.”表示

WBOY
Release: 2016-06-13 12:09:45
Original
1542 people have browsed it

php中如何对输出的文章限制字数,剩余的用“...”表示?
各位哥哥,小女子刚学php,在对文章内容进行输出时,如图:
由于文章内容太多,搞得整个表格都变形了,所以就想通过只输出一部分,其他内容用省略号表示,如图:
也在网上找了很多方法都行不通的,CSS啊,php中substr等函数那些都搞不好,希望能得到帮助!谢谢
------解决思路----------------------

echo mb_strlen($news['n_content'], 'utf-8') > 9 ? mb_substr($news['n_content'], 0, 9, 'utf-8').'....' : $news['n_content'];
Copy after login

------解决思路----------------------
楼上的要确保楼主打开了extension=php_mbstring.dll ;
<br />/**<br /> * 字符串截取<br /> *<br /> */<br />function sub_str($str, $length = 0, $append = true)<br />{<br />    $str = trim($str);<br />    $strlength = strlen($str);<br /><br />    if ($length == 0 <br><font color='#FF8000'>------解决思路----------------------</font><br> $length >= $strlength)<br />    {<br />        return $str;  //截取长度等于0或大于等于本字符串的长度,返回字符串本身<br />    }<br />    elseif ($length < 0)  //如果截取长度为负数<br />    {<br />        $length = $strlength + $length;//那么截取长度就等于字符串长度减去截取长度<br />        if ($length < 0)<br />        {<br />            $length = $strlength;//如果截取长度的绝对值大于字符串本身长度,则截取长度取字符串本身的长度<br />        }<br />    }<br /><br />    if (function_exists('mb_substr'))<br />    {<br />        $newstr = mb_substr($str, 0, $length, EC_CHARSET);<br />    }<br />    elseif (function_exists('iconv_substr'))<br />    {<br />        $newstr = iconv_substr($str, 0, $length, EC_CHARSET);<br />    }<br />    else<br />    {<br />        //$newstr = trim_right(substr($str, 0, $length));<br />        $newstr = substr($str, 0, $length);<br />    }<br /><br />    if ($append && $str != $newstr)<br />    {<br />        $newstr .= '...';<br />    }<br /><br />    return $newstr;<br />}<br />
Copy after login

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