Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/CSS在字符串中查找两个颜色词,更改其颜色_Php_Css - Fatal编程技术网

PHP/CSS在字符串中查找两个颜色词,更改其颜色

PHP/CSS在字符串中查找两个颜色词,更改其颜色,php,css,Php,Css,PHP/CSS在字符串中找到两个单词,更改其显示颜色。 有问题,找不到解决方案,有什么建议吗?谢谢 <?php $word = '<font color = "blue">blue</font>'; $num = '<font color = "blue">123</font>'; $text = $word.$num; echo '$text='.$text.'<br>'; $blue='blue'; $find = '<

PHP/CSS在字符串中找到两个单词,更改其显示颜色。 有问题,找不到解决方案,有什么建议吗?谢谢

<?php
$word = '<font color = "blue">blue</font>';
$num = '<font color = "blue">123</font>';
$text = $word.$num;
echo '$text='.$text.'<br>';

$blue='blue';
$find = '<font color="blue">'.$blue.'</font>';
$re='<font color="green">'.$blue.'</font>';

$check = str_replace($find,$re,$text);

echo '$find='.$find.'<br>';
echo '$re='.$re.'<br>';
echo '$check='.$check.'<br>';
?>

试试这个。我想这会解决你的问题

<?php
$word = '<font color = "blue">blue</font>';
$num = '<font color = "blue">123</font>';
$text = $word.$num;
echo $text.'='.$text.'<br>';

$blue='blue';
$find = '<font color="blue">'.$blue.'</font>';
$re='<font color="green">'.$blue.'</font>';

$check = str_replace($find,$re,$text);

echo $find.'='.$find.'<br>';
echo $re.'='.$re.'<br>';
echo $check.'='.$check.'<br>';
?>

它不起作用,因为原始文本中“=”前后都有空格:

color = "blue"
并且在
$find
中没有空格:

color="blue"
要避免这种情况,请使用正则表达式:

<?php
$word = '<font color = "blue">blue</font>';
$num = '<font color = "blue">123</font>';
$text = $word.$num;

$blue='blue';
$find = '<font color.*?=.*?"blue">'.$blue.'<\/font>';
$re='<font color="green">'.$blue.'</font>';

$check = preg_replace("/$find/", $re, $text);

echo '$check='.$check."<br>\n";
?>

请提供有关您的问题的更多详细信息。该怎么办?会发生什么?
$check=<font color="green">blue</font><font color = "blue">123</font><br>