My search in made in mysql, but I want to use PHP to high lite match words either by making them bold or wrapping them with a <span style=...> .

How do I compare my $keywords to the $search_data string and then wrap the matched words in bold?

$keywords = "highlite search word";

$search_data = "I want to highlite each word that is found from 
	a group of words during a search of unhighlited data.";

// the results I want are:
$search_results = "I want to <b>highlite</b> each <b>word</b> that is found from
	a group of words during a <b>search</b> of unhighlited data.";

echo $search_results;

Sam
;

    $keywords = "highlite search word";
    
    $search_data = "I want to highlite each word that is found from
        a group of words during a search of unhighlited data.";
    
    $searchWords = explode(' ', $keywords);
    array_walk($searchWords, create_function('&$v, $k', '$v = "/\\b$v\\b/i";'));
    $search_results = preg_replace($searchWords, '<strong>\\0</strong>', $search_data);
    
    echo $search_results;
    

      That is exactly what I needed to work with.

      Thanks!

        I would offer one minor suggestion:

        If you don't want to enable regexp syntax in your searches, I would use [man]preg_quote/man like so:

        array_walk($searchWords, create_function('&$v, $k', '$v = "/\\b'.preg_quote($v, '/').'\\b/i";'));

        This ensures that your keywords won't break the regexp syntax if you use the special regexp characters.

          Write a Reply...