dongsang6899
2015-06-19 06:33 阅读 38
已采纳

使用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 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.

    点赞 评论 复制链接分享
  • dounan9070 dounan9070 2015-06-19 07:45

    here is my code

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

    Here is the output 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>
    

    Showing like this in browser

    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
    
    点赞 评论 复制链接分享

相关推荐