Limit to number of charaters in the string is much easier coding.
Now, my guess is one 'word' is at average 4-5 chars long.
So, limit the string to 150 chars would mostly give like 30 words.
This is my testscript while I was working at this your case
<?php
$text = 'The snippet works. It does return a string based on which column is assigned into $mess. What I have in mind is to replace the content of $mess into a string that contains the keyword matches in each column of the database. Like a footprint should I say, a trace of its process.';
echo strlen($text);
echo '<br>';
// my oneliner
$str = substr($text,0,strrpos(substr($text,0,150),' ')).'…';
echo $text;
echo '<br>';
echo $str;
?>
This is my chopstring() function to use
<?php
function chopstring($text, $length='150'){
if(strlen($text) <= $length)
return $text;
$str = substr($text, 0, $length);
$len = strrpos($str, ' ');
return substr($str, 0, $len).'…';
}
// Example
$text = 'The snippet works. It does return a string based on which column is assigned into $mess. What I have in mind is to replace the content of $mess into a string that contains the keyword matches in each column of the database. Like a footprint should I say, a trace of its process.';
echo chopstring( $text, 100 ); // to 100 characters
echo '<br>';
echo chopstring( $text ); // to default 150 characters
echo '<br>';
echo chopstring( $text, 200 ); // to 200 characters
?>