dongsang6899 2015-06-19 06:33
浏览 81
已采纳

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

How to remove unwanted

tags using pregmatch in below cases. i wrote pregmatch but its not working for some cases. here is my string

<?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);

?>

Below are unwanted paragraphs. so how can i remove from string

<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条回答 默认 最新

  • duan051347 2015-06-19 07:02
    关注

    In your regexp in the part <\br [^>]*> you escape the "b" with a backslash. By that you make it a backspace. I think you don't want that.

    Try to remove that backslash which then makes it:

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

    EDIT: (because of new information by the questioner) OK, with this one it works:

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

    Had to replace the square brackets by round ones and make the slash before "br" optional.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏