Why use a regexp? Does anyone think that's faster? (Hey, not criticism, I'd really like to know, myself...)
OTOH, is this anything like what you want? from user-contrib notes at manual on 'substr'...
function substr_index($text, $maxChars = 255, $splitter = ' ') {
$theReturn = $text;
$lastSpace = false;
// only do the rest if we're over the character limit
if (strlen($text) > $maxChars) {
$theReturn = substr($text, 0, $maxChars - 1);
// add closing punctuation back in if found
if (in_array(substr($text, $maxChars - 1, 1), array(' ', '.', '!', '?'))) {
$theReturn .= substr($text, $maxChars, 1);
} else {
// make room for splitter string and look for truncated words
$theReturn = substr($theReturn, 0, $maxChars - strlen($splitter));
$lastSpace = strrpos($theReturn, ' ');
// Remove truncated words and trailing spaces
if ($lastSpace !== false) {
$theReturn = substr($theReturn, 0, $lastSpace);
}
// append the splitter string
$theReturn .= $splitter;
}
}
return $theReturn;
}?>
As far as a regular expression, I have to make a guess "[.]{1,255}[.|!|?|;|😐 ]$" ??