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 = '<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>
+0
thx〜它工作得很好! – user3901528
答
试试这个。我认为这将解决您的问题
<?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>';
?>
请对您的问题的详细信息。应该发生什么?什么发生呢? – Renzo