Ограничить количество слов в тексте:
function limitTextWords($content = false, $limit = false, $stripTags = false, $ellipsis = false)
{
if ($content && $limit) {
$content = ($stripTags ? strip_tags($content) : $content);
$content = explode(' ', $content, $limit+1);
array_pop($content);
if ($ellipsis) {
array_push($content, '...');
}
$content = implode(' ', $content);
}
return $content;
}
Предельные символы в тексте:
function limitTextChars($content = false, $limit = false, $stripTags = false, $ellipsis = false)
{
if ($content && $limit) {
$content = ($stripTags ? strip_tags($content) : $content);
$ellipsis = ($ellipsis ? "..." : $ellipsis);
$content = mb_strimwidth($content, 0, $limit, $ellipsis);
}
return $content;
}
Использование:
$text = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.";
echo limitTextWords($text, 5, true, true);
echo limitTextChars($text, 5, true, true);