PHP/CSS在字符串中查找两个颜色词,更改其颜色
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
$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>