在变量内搜索某个术语,php

  1. I am pulling profiles which mention the word "fitness" from a database like so:

    $result = mysql_query("SELECT profile FROM ".$table." WHERE profile LIKE ('fitness')");
    
  2. I am doing a basic retrieval of the $profile variable and echo it.

    while($slice = mysql_fetch_assoc($result)) {
        $profile = $slice['profile'];
        echo $profile;
    }
    

What I'd like is a way to search the text inside $profile for the word "fitness" and highlight the found word using maybe CSS.

Any ideas how?

UPDATE:

Ty very much, and I have one more problem.

In the sql query I will have:

$result = mysql_query("SELECT profile FROM ".$table." WHERE $profile_rule");

$profile_rule = 'profile LIKE ('fitness') AND profile LIKE ('pets')';

Is there a way to s**trip the $profile_rule** and get just the words fitness and pets? Maybe strip everything which isn't encased by '' ?

Ty

function highlightWord($word, $text){
    return str_replace($word, '<span class="highlighted">' . $word . '</span>', $text);
}

$profile = highlightWord('fitness', $profile);

EDIT: Per your update, take a look at the IN() operator:

$result = mysql_query("SELECT profile FROM $table WHERE profile IN ('fitness','pets')");

EDIT 2: I think I misunderstood your question, though I still recommend edit 1. If you just want to grab the words fitness and pets from that $profile_rule string, use some simple regex:

preg_match_all("|[a-z]+(?=\'\)|","profile LIKE ('fitness') AND profile LIKE ('pets')", $profiles);
var_dump($profiles); // array(1) { [0]=> array(2) { [0]=> string(7) "fitness" [1]=> string(4) "pets" } }
$profile = str_replace("fitness", "<span style=\"color:red\">fitness</span>",
           $slice["profile"]);