Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.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 如何替换样式并将其添加到特定标记中_Php_Regex - Fatal编程技术网

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

Php 如何替换样式并将其添加到特定标记中,php,regex,Php,Regex,我有个问题。我不能替换和添加内联样式的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> &

我有个问题。我不能替换和添加内联样式的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>
hello world

测试

我想要这个 输出:

<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>
hello world

测试

我试过这种方法

$re=“/(]+\”([^>\“]+)/miu”;
$str=“

hello world

\n

测试

\n\n

”; $subst=“$1$2margin:0;”; $result=preg_replace($re,$subst,$str);

任何人都请帮助我。谢谢。

我已经解决了这个问题:

<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>

按正则表达式模式搜索((?您是否考虑过使用
DOM
?不,只有preg_replace。最后2行不匹配&不替换。请查看演示为什么您不能自己修改字符串?@self我不知道高级正则表达式