用新风格替换内嵌式

时间:2017-09-25 09:59:43

标签: php regex preg-replace preg-match

  $string = "<span style='position:absolute'>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
 tempor incididunt ut labore et dolore magna aliqua.</span> Ut enim ad minim veniam,
 quis nostrud exercitation <span style='color:white'>ullamco laboris nisi ut aliquip ex ea commodo color:white
 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
 cillum dolore eu fugiat nulla pariatur.</span> Excepteur sint occaecat cupidatat non
 proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

我想替换和结果,只是内联并非所有匹配的字符串

$string = "<span style='position:relative'>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
 tempor incididunt ut labore et dolore magna aliqua.</span> Ut enim ad minim veniam,
 quis nostrud exercitation <span style='color:black'>ullamco laboris nisi ut aliquip ex ea commodo color:white
 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
 cillum dolore eu fugiat nulla pariatur.</span> Excepteur sint occaecat cupidatat non
 proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

任何进一步的建议表示赞赏。我不知道你是如何设法让它正常工作的!

1 个答案:

答案 0 :(得分:0)

我不确定总要求,但希望它能帮到你。您可以使用str_replacepreg_replace。所以这是代码

<强>选项1。

$var = "<h2 style='color:blue;'>I want this heading to be blue instead of default red</h2>";

$newphrase = str_replace('color:blue;', 'color:red;', $var);
echo $newphrase;

<强> 1选项。

$newphrase = preg_replace("/:blue/", ":red", $var);
echo $newphrase;

并且都提供相同的输出

enter image description here