php正则表达式删除html标签

$str='<p align="center"><img src="1.jpg" width="50" height="50"/></p><p>第一段</p><p align="center"><img src="2.jpg" width="50" height="50"/></p><p>第二段</p>';

我是网页采集的,需要把采集到的内容把<p align="center"></p>这个标签,及他里面的图片img这个标签删了,请高手帮忙下写正则,万分感谢哦。
我最后要得到的结果是:<p>第一段</p><p>第二段</p>
用php采集的,麻烦会正则的帮我解决下。

$str='<p align="center"><img src="1.jpg" width="50" height="50"/></p><p>第一段</p><p align="center"><img src="2.jpg" width="50" height="50"/></p><p>第二段</p>';
preg_match_all('/<p>(.*?)<\/p>/', $str, $matches);
print_r($matches);
温馨提示:内容为网友见解,仅供参考
第1个回答  2016-07-21
1.htmlspecialchars()
查查怎么用的,不过正则也可以

 2.过滤字符串中所有的html标签有两种方法一种是我们自己写一个函数,用正则过滤,一个是用php自带函数strip_tags


function clear_html_label($html)
{
$search = array
("'<script[^>]*?>.*?</script>'si",
"'<[/!]*?[^<>]*?>'si", "'([rn])[s]+'",
"'&(quot|#34);'i", "'&(amp|#38);'i", "'&(lt|#60);'i",
"'&(gt|#62);'i", "'&(nbsp|#160);'i", "'&(iexcl|#161);'i",
"'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i",
"'&#(d+);'e");
$replace = array ("", "", "1", """, "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(1)");

return preg_replace($search, $replace, $html);
}

//实例应用

echo clear_html_label($string);

第2个回答  2016-07-16
#<p>(.*)</p>#iU
相似回答