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