How to limit the number of words in PHP to the output of the article, the remaining use of "." Said

Source: Internet
Author: User
How to limit the number of words in PHP to the output of the article, the rest of the "..." means?
Dear brother, The little girl just learned PHP, when the content of the article output,
Because of the content of the article too much, make the entire table is deformed, so I want to pass only part of the output, the other content with an ellipsis,
Also found on the Internet a lot of methods are not feasible, CSS Ah, PHP substr and other functions are not good, hope to get help! Thank you
------to solve the idea----------------------
Echo Mb_strlen ($news [' n_content '], ' Utf-8 ') > 9? Mb_substr ($news [' n_content '], 0, 9, ' Utf-8 '). ' ..... ': $news [' n_content '];

------to solve the idea----------------------
Upstairs to ensure that the landlord opened the Extension=php_mbstring.dll;

/**
* String interception
*
*/
function Sub_str ($str, $length = 0, $append = True)
{
$str = Trim ($STR);
$strlength = strlen ($STR);

if ($length = = 0
------to solve the idea----------------------
$length >= $strlength)
{
return $str; Intercept length equal to 0 or greater than or equal to the length of this string, return the string itself
}
ElseIf ($length < 0)//If intercept length is negative
{
$length = $strlength + $length;//Then The intercept length is equal to the length of the string minus the intercept length
if ($length < 0)
{
$length = $strlength;//If the absolute value of the intercept length is greater than the length of the string itself, the length of the string itself is truncated
}
}

if (function_exists (' mb_substr '))
{
$newstr = mb_substr ($str, 0, $length, ec_charset);
}
ElseIf (function_exists (' iconv_substr '))
{
$newstr = iconv_substr ($str, 0, $length, ec_charset);
}
Else
{
$newstr = Trim_right (substr ($str, 0, $length));
$newstr = substr ($str, 0, $length);
}

if ($append && $str! = $newstr)
{
$newstr. = ' ... ';
}

return $newstr;
}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.