I'm trying to make excerpts from long texts while deleting everything that comes after the last space if the string length is more than 110 characters.
$string = 'Стихи похожи на людей: помнят прошлое и ничего не знают о будущем, хотят жить вечно, a страница уже перелистывается.';
if (mb_strlen($string ) > 110) {
$pos = mb_strpos($string , ' ', 110);
$excerpt = rtrim(mb_substr($string, 0, $pos), '.,—-_!@\'"()*#~').'...';
}
If I print with print_r(mb_strlen($pos));
the result of $pos
is 0
, and its working correctly if I change $pos
to $pos = mb_strpos($quote_content, ' ', 99);
.
The last word in this case is 16 characters long and the whole string is 116 characters long so it makes perfect sense as to why a 99 offset works while anything above will result a $pos
value of 0
thus instead of making an excerpt it just returns ...
(based on the current example).
I have quite a few strings here with different string length and words length so I need a dynamic solution that will work in all cases. Any ideas?