Can anyone tell me how to get the last sentence from a chunk of text. The text is likely to be at least one sentence, or a good few paragraphs. Also, rather annoyingly it could well end in a '...'.
Here's what I have managed to cobble together, but given more than one full stop at the end it's not going be much good.
function last_sentence($content) {
$pos = strrpos($content, '.');
$pos_two = strrpos($content, '.', $pos - strlen($content) - 1);
if($pos_two === false) {
return $content;
}
else {
return substr($content, $pos_two+1);
}
} // end function
So in essence if
$my_text="This is the first sentence. The second is here. And the third trails off in a frustrating fashion......"
Any ideas?
$my_text = array_pop(array_filter(explode('.', $my_text), 'trim'));
Convert the string to an array, remove any empty values, then return the last item in the array.