这不行吗?
function filter_ptags_on_images($content){
return preg_replace('/\<p\b.*?\>(.+)\<\/p\>/i', '$1', $content);
}
编辑:
我的错,对不起,我没有仔细阅读
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;
}
编辑:
这是我认为的另一个更好的版本:
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);
}
编辑:
这是更好的版本:
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);
}