使用php从字符串中删除不需要的段落

时间:2015-06-19 06:33:37

标签: php parameters line paragraphs

如何在以下情况下使用pregmatch删除不需要的

标记。我写了pregmatch但它在某些情况下不起作用。这是我的字符串

<?php 

$str='<div class="borderdummydiv" style="padding: 10px;">
<div class="showcolsec" style="line-height: normal;">
<p style="font-size: 14px;" data-mce-style="font-size: 14px;"><strong>Email Template for Testing - 01:</strong></p><p style="font-size: 14px;" data-mce-style="font-size: 14px;"><br data-mce-bogus="1"></p><p style="font-size: 14px;" data-mce-style="font-size: 14px;">Email body for testing the autoresponder mails and email blasts scheduling functionality<br data-mce-bogus="1"></p>
<p></p>
<p><br></p>
<p></br></p>
<p>&nbsp; </br></p>
<p>&nbsp; <br class="ddd"></p>
<p class="ddd"></p>
<p class="ddd"><br class="ddd"></p>
<p class="ddd"></br></p>
<p class="ddd">&nbsp; </br></p>
<p class="ddd">&nbsp; <br class="ddd"></p>
</div>
</div>';
//echo $str;

echo preg_replace("/<p[^>]*>[\s|&nbsp;|<\br [^>]*>|<\/br>]*<\/p>/", '', $str);

?>

以下是不需要的段落。那么如何从字符串中删除

<p></p>
<p><br></p>
<p></br></p>
<p>&nbsp; </br></p>
<p>&nbsp; <br class="ddd"></p>
<p class="ddd"></p>
<p class="ddd"><br class="ddd"></p>
<p class="ddd"></br></p>
<p class="ddd">&nbsp; </br></p>
<p class="ddd">&nbsp; <br class="ddd"></p>

2 个答案:

答案 0 :(得分:1)

在部分&lt; \ br [^&gt;] *&gt;中的正则表达式中你用反斜杠逃避“b”。 通过它,你使它成为一个退格。我想你不想那样。

尝试删除后面的反斜杠:

echo preg_replace("/<p[^>]*>[\s|&nbsp;|<br [^>]*>|<\/br>]*<\/p>/", '', $str);

编辑:(由于提问者的新信息) 好吧,有了它,它可以工作:

echo preg_replace("/<p[^>]*>(\s|&nbsp;|<br [^>]*>|<\/?br>)*<\/p>/", '', $str);

必须用圆形方括号替换方括号,并在“br”可选之前做斜线。

答案 1 :(得分:0)

这是我的代码

$str='<div class="borderdummydiv" style="padding: 10px;">
<div class="showcolsec" style="line-height: normal;">
<p style="font-size: 14px;" data-mce-style="font-size: 14px;"><strong>Email Template for Testing - 01:</strong></p>
<p style="font-size: 14px;" data-mce-style="font-size: 14px;"><br data-mce-bogus="1"></p>
<p></p>
<p><br></p>
<p></br></p>
<p>&nbsp; </br></p>
<p>&nbsp; <br class="ddd"></p>
<p class="ddd"></p>
<p class="ddd"><br class="ddd"></p>
<p class="ddd"></br></p>
<p class="ddd">&nbsp; </br></p>
<p class="ddd">&nbsp; <br class="ddd"></p>
<p style="font-size: 14px;" data-mce-style="font-size: 14px;">Email body for testing the autoresponder mails and email blasts scheduling functionality<br data-mce-bogus="1"></p>

<p style="font-size: 14px;" data-mce-style="font-size: 14px;"><strong>Email Template for Testing - 01:</strong></p><p style="font-size: 14px;" data-mce-style="font-size: 14px;"><br data-mce-bogus="1"></p><p style="font-size: 14px;" data-mce-style="font-size: 14px;">Email body for testing the autoresponder mails and email blasts scheduling functionality<br data-mce-bogus="1"></p>
</div>
</div>';

//echo $str;
echo preg_replace("/<p[^>]*>[\s|&nbsp;|<br [^>]*>|<\/br>]*<\/p>/", '', $str);

这是输出html

<div class="borderdummydiv" style="padding: 10px;">
<div class="showcolsec" style="line-height: normal;">
<p style="font-size: 14px;" data-mce-style="font-size: 14px;"><strong>Email Template for Testing - 01:</strong></p>
<p style="font-size: 14px;" data-mce-style="font-size: 14px;"><br data-mce-bogus="1"></p>
<p></p>
</p>
<p>
<p>&nbsp; 
<p>&nbsp; <br class="ddd"></p>
<p class="ddd"></p>
<p class="ddd"><br class="ddd"></p>
<p class="ddd">
<p class="ddd">&nbsp; 
<p class="ddd">&nbsp; <br class="ddd"></p>
<p style="font-size: 14px;" data-mce-style="font-size: 14px;">Email body for testing the autoresponder mails and email blasts scheduling functionality<br data-mce-bogus="1"></p>

<p style="font-size: 14px;" data-mce-style="font-size: 14px;"><strong>Email Template for Testing - 01:</strong></p><p style="font-size: 14px;" data-mce-style="font-size: 14px;"><br data-mce-bogus="1"></p><p style="font-size: 14px;" data-mce-style="font-size: 14px;">Email body for testing the autoresponder mails and email blasts scheduling functionality<br data-mce-bogus="1"></p>
</div>
</div>

在浏览器中显示如下内容

Email Template for Testing - 01:











Email body for testing the autoresponder mails and email blasts scheduling functionality

Email Template for Testing - 01:


Email body for testing the autoresponder mails and email blasts scheduling functionality