如何替换样式并将其添加到特定标记中


how to replace and add style into specific tag

我有一个问题。 我无法替换和添加内联样式的 CSS。我有这个 输入:

<p  style="text-align: justify;"><span style="font-size: 11pt;">hello world<br /></span></p>
<p id="tem" style="text-align: justify;">test</p>
<p >&nbsp;</p>
<p>&nbsp;</p>

我想要这个输出:

<p style="text-align: justify;margin:0;"><span style="font-size: 11pt;">hello world<br /></span></p>
<p id="tem" style="text-align: justify;margin:0;">test</p>
<p  style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;</p>

我已经尝试过这种方式演示

 $re = "/(<p[^>]+'")([^>'"]+)/miu";
    $str = "<p id='"tem'" style='"text-align: justify;'"><span style='"font-size: 11pt;'">hello world<br /></span></p>'n<p style='"text-align: justify;'">test</p>'n<p >&nbsp;</p>'n<p>&nbsp;</p>";
    $subst = "$1$2margin:0;";
    $result = preg_replace($re, $subst, $str);

请帮助我。谢谢。

我已经解决了:

按正则表达式模式((?<='<)[^>]+style=")([^>"]+)搜索并替换为$1$2margin:0;

现场演示