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