Never mind. I found a way to make it work.
$complete_row_text = "Phasellus posuere sapien id sapien interdum auctor. Fusce et justo. Morbi ac arcu vitae erat viverra egestas. In orci. In rutrum purus sed erat. Cras commodo massa non tortor. Vivamus feugiat. Proin ac nisl. Proin in enim sed lorem imperdiet dapibus. Vivamus eget sem.";
// the last paramenter in substr();
$how_long = strlen($complete_row_text);
$search_word = "Cras commodo";
// the first paramenter in substr();
$position = strrpos($complete_row_text,$search_word);
echo $position . " -> ";
echo substr($complete_row_text,$position-10,200);
and now it display text like Google do 🆒
There is two other functions that could be added
function truncate_string($details,$max)
{
if(strlen($details)>$max)
{
$details = substr($details,0,$max);
$i = strrpos($details," ");
$details = substr($details,0,$i);
$details = $details."<b>...</b>";
}
return $details;
}
and
function bold($subject, $word)
{
$mywords = explode(" ",$word);
for ($j=0;$j<count($mywords);$j++)
{
$regex_chars = '\.+?(){}[]^$';
for ($i=0; $i<strlen($regex_chars); $i++)
{
$char = substr($regex_chars, $i, 1);
$mywords[$j] = str_replace($char, '\\'.$char, $mywords[$j]);
}
$mywords[$j] = '(.*)('.$mywords[$j].')(.*)';
$subject = eregi_replace($mywords[$j], '\1<b>\2</b>\3', $subject);
}
return $subject;
}
the result with search word "Cras commodo" will look just like this:
[FONT="Georgia"]...sed erat. Cras commodo massa non tortor. Vivamus feugiat. Proin ac nisl. Proin in enim sed lorem imperdiet dapibus. Vivamus eget sem.[/FONT]