查找单词并更改颜色

时间:2018-09-19 08:53:09

标签: php html css

我想使用CSS和条件更改在ECHO函数中找到的文本的颜色。

例如:

我使用ECHO函数调用文本:

echo ": The net value of the entire order"
echo ": The gross value of the entire order"
echo ": The gross value of the entire order"
echo ": The net value of the entire order"

并使用条件“ if”找到单词“ net”或“ gross”,然后将颜色更改为红色或蓝色。

该怎么做?我一直在寻找论坛,但帖子仅用于变量的赋值,我想通过ECHO函数更改颜色,这似乎毫无意义,但是如果有成千上万个这样的单词呢?

我正在寻求您的帮助, 我是这种语言的新手。

3 个答案:

答案 0 :(得分:5)

$str = ": The net value of the entire order"

$str = str_replace('net', '<span style="color:red">net</span>', $str);
$str = str_replace('gross', '<span style="color:blue">gross</span>', $str);

echo $str;

答案 1 :(得分:0)

$string = "The gross value of the entire order";
$wordOptions = [
    "net" => "#aaaaaa",
    "gross" => "#bbbbbb"
];

foreach($wordOptions as $word => $color){
    str_replace($word, "<span style=\"color: {$color};\">{$word}</span>", $string);
}

答案 2 :(得分:0)

$str="The net value of the entire order";

preg_replace('/\b(net)\b/i', '<span style="color:red;">net</span>', $str);