Regular Express to find empty <p> tags using PHP
Posted on 2012-03-17
I've never been real good with regular expressions...maybe someone can help. I need to find all instances of empty <p></p> tags in my content and replace with <p> </p> so they correctly do something (the style in the css file has margin:0; padding:0 for p and with nothing in them, they end up not giving even a line break.
In that easy case, I'm using str_ireplace("<p></p>","<p> </p>",$data);
The problem comes in when there is an empty <p> tag that includes some styling info that's coming from the TinyMCE editor, such as
<p style="text-align: center;"></p>
I still need to be able to find these. The other problem is there may be white space in between the tags such as:
<p style="text-align: center;"> </p>
which also don't display anything in the browser.
So, my goal would be to find all <p tags with any number of additional attributes on the tag, followed by a closing bracket > followed by any number of whitespace characters (or at least spaces) followed by a closing </p> tag and replace with the same opening tag, </p>.
<p></p> ==> <p> </p>
<p> </p> ==> <p> </p>
<p style="text-align: center;"></p> ==> <p style="text-align: center;"> </p>
There may be "n" number of these situations in any data and they will be dispersed throughout the data as this is the content portion of a webpage, so there may be lots of paragraphs within a full page.
Can anybody help with this?