Search code examples
phpregexwordpresshtml-parsingsanitization

Remove parent <p> tag if its only child is an <img> tag


I have some HTML with unwanted <p> tags around <img> tags.

I would like to remove the parent <p> tags that wrap <img> tags.

Wordpress is annoying, essentially. If it's easier with str_replace() or something, let me know. But I've tried. And failed so far....

Input HTML

<p>This will serve as a debug page.</p>
<p><img src="http://mattmueller.me/blog/wp-content/uploads/2009/12/threadless.png" alt="Threadless" title="Threadless" width="650" height="150" class="alignnone size-full wp-image-73" /></p>

Desired output HTML:

<p>This will serve as a debug page.</p>
<img src="http://mattmueller.me/blog/wp-content/uploads/2009/12/threadless.png" alt="Threadless" title="Threadless" width="650" height="150" class="alignnone size-full wp-image-73" />

Solution

  • $x = preg_replace('/<p[^>]*>(<img[^>]*>)<\/p[^>]*>/', '$1', $x);
    

    Where $x is your content