Page 1 of 1

Highlighting search terms

Posted: Fri Jun 25, 2004 2:07 am
by Calimero
If user inputs words into a textfield to search for,

What is the code for highlighting them in the results page?

Thanks Ahead!

Posted: Fri Jun 25, 2004 2:18 am
by markl999
Here's an example you should be able to modify to suite:

Code: Select all

<?php
$results = 'one two three four five';
$search = 'three five six';
foreach(explode(' ', $search) as $word){
  $results = str_replace($word, '<span style="background-color:#f00">'.$word.'</span>', $results);
}
echo $results;
?>