在变量内搜索特定术语php

在变量内搜索特定术语php,php,Php,我从数据库中提取提到“健身”一词的个人资料,如下所示: $result = mysql_query("SELECT profile FROM ".$table." WHERE profile LIKE ('fitness')"); 我正在对$profile变量进行基本检索,并对其进行回显 while($slice = mysql_fetch_assoc($result)) { $profile = $slice['profile']; echo $profile; } 我

我从数据库中提取提到“健身”一词的个人资料,如下所示:

$result = mysql_query("SELECT profile FROM ".$table." WHERE profile LIKE ('fitness')");
  • 我正在对
    $profile
    变量进行基本检索,并对其进行回显

    while($slice = mysql_fetch_assoc($result)) {
        $profile = $slice['profile'];
        echo $profile;
    }
    
  • 我想要的是一种在
    $profile
    中搜索单词“fitness”并使用CSS突出显示找到的单词的方法

    你知道怎么做吗

    更新:

    非常抱歉,我还有一个问题

    在sql查询中,我将有:

    $result = mysql_query("SELECT profile FROM ".$table." WHERE $profile_rule");
    
    $profile_rule = 'profile LIKE ('fitness') AND profile LIKE ('pets')';
    
    有没有办法跳过$profile_规则**,只获得“健身”和“宠物”这两个词?可能会剥掉所有没有被“”包围的东西

    Ty

    $profile=str_替换(“健身”、“健身”,
    $slice[“profile”]);
    
    编辑2:我想我误解了你的问题,尽管我仍然建议编辑1。如果您只想从
    $profile\u规则
    字符串中获取单词
    fitness
    pets
    ,请使用一些简单的正则表达式:

    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" } }
    
    非常感谢,泰:)你能看看问题的最新进展吗,也许你能给我一个想法?
    function highlightWord($word, $text){
        return str_replace($word, '<span class="highlighted">' . $word . '</span>', $text);
    }
    
    $profile = highlightWord('fitness', $profile);
    
    $result = mysql_query("SELECT profile FROM $table WHERE profile IN ('fitness','pets')");
    
    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" } }