自动从HTML中删除“p”标记

I have the following code, which supposedly removes all p tags that are wrapped around images. I am literally copying and pasting this into my functions.php. However, it's not working:

function filter_ptags_on_images($content){
   return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}

add_filter('the_content', 'filter_ptags_on_images');

Do I need to change the function parameter with something more relevant to my theme? I'm new to WordPress so apologies for what might seem like a silly question.

wouldn't this work?

function filter_ptags_on_images($content){
   return preg_replace('/\<p\b.*?\>(.+)\<\/p\>/i', '$1', $content);
}

edit:

my bad, sorry I wasn't reading carefully

function filter_ptags_on_images($content){
   if (!preg_match_all('/\<p\b.*?\>(.+?)\<\/p\>/is',$content,$ps)) {
      return $content;
   }

   $new_content = $content;

   foreach ($ps[0] as &$p) {
      if (strpos($p,"<img ")) {
         $p_stripped_chunk = preg_replace('/\<p\b.*?\>(.*?\<img\b.+\>.*?)\<\/p\>/is', '$1', $p);
         $new_content = str_replace($p,$p_stripped_chunk,$new_content);
      }
   }

   return $new_content;
}

edit:

this is another better version I think:

function filter_ptags_on_images($content){
   if (!preg_match_all('/\<p\b.*?\>(.*?)\<\/p\>/is',$content,$ps_with_image)) {
      return $content;
   }

   foreach ($ps_with_image[0] as $match_x => $p) {
      if (!stripos($p,'<img')) {
         unset($ps_with_image[0][$match_x],$ps_with_image[1][$match_x]);
      }
   }

   return str_replace($ps_with_image[0], $ps_with_image[1], $content);
}

edit:

this is much much better version of this:

function filter_ptags_on_images($content){
   if (!preg_match_all('/\<p\b[^\>]*?\>(([^\<]|\<(?!\/p))*?\<img\b.+?\>.*?)\<\/p\>/is',$content,$ps_with_image)) {
      return $content;
   }
   return str_replace($ps_with_image[0], $ps_with_image[1], $content);
}
var context = $("p > img");

for (var i = 0; i < context.length; i++) {
    $(context[i]).remove();
}

This will remove all the <p> tags around images along with their content.

filter the P tags from the images and iFrame

function filter_ptags_on_images($content)
{
$content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
}
add_filter('the_content', 'filter_ptags_on_images');

You can disable this feature by adding this code in your functions.php

remove_filter( 'the_content', 'wpautop' );  // Disable auto 'p' in content

remove_filter( 'the_excerpt', 'wpautop' ); // Disable auto 'p' in excerpt

remove_filter()

wpautop