private function getExcerpt($text, $length = 250, $separator = ' …')
{
// use regex instead of strip_tags to left some spaces when removing tags
$text = preg_replace('#<[^>]+>#', ' ', $text);
// trim whitespace at beginning or end of string
// See: http://stackoverflow.com/a/4167053/569101
$text = preg_replace('/^[\\pZ\\pC]+|[\\pZ\\pC]+$/u', '', $text);
// clean new lines and tabs
$text = trim(preg_replace("/[\n\r\t ]+/", ' ', $text), ' ');
if (mb_strlen($text) > $length) {
// If breakpoint is on the last word, return the text without separator.
if (false === ($breakpoint = mb_strpos($text, ' ', $length))) {
return $text;
}
$length = $breakpoint;
return rtrim(mb_substr($text, 0, $length)) . $separator;
}
return $text;
}