Remove unwanted paragraphs from string using php

404 Views Asked by At

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

There are 2 best solutions below

3
On BEST ANSWER

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.

0
On

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