2017-04-23 46 views
1

我想删除所有img标签的字符串,如果不`吨喜欢我的形式,但我不能正则表达式查找所有img标签不和取出

例子(我想留在我的字符串):

<img src="http://localhost/uploads/user1/test3.jpg" title="test" class="img-responsive img-maxheight"> 
<img src="http://localhost/uploads/user2/test1.jpg" alt="test" class="img-responsive img-maxheight"> 
<img src="http://localhost/uploads/user3/test2.jpg" class="img-responsive img-maxheight"> 

,并在PHP我正则表达式的代码是:

$pattern ="/<img src=\"http:\/\/localhost\/(uploads|thumbs)\/$user\/(.*?)\" (alt|title|)=\"(.*?)\" class=\"img-responsive img-maxheight\">/i"; 

$replacement = ' '; 
$content = preg_replace($pattern, $replacement, $content); 
+1

为什么使用正则表达式? ? – Akintunde007

+0

[RegEx match open tags not except XHTML self-contained tags]可能重复(http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Pharaoh

回答

0

一个可能的模式的最短的版本将是/<img[^>]*>/
这可能在大部分时间都有效。好一点的是:/<\simg[^>]*>/。它也将与< img ......>一起使用。
您可以插入很多角落案例来改进模式,但更好的解决方案是not to use a regex in the first place
相反,您应该通过专用解析器解析html:How do you parse and process HTML/XML in PHP?

+0

查看相似保留并删除其余部分.keep: test 其他删除。 – user2790938

+0

请回答我的问题,不要提问?!!!!! $ pattern =“/ /i”; 如果不喜欢花样,则从字符串中删除。 – user2790938

相关问题