2017-04-23 45 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

相關問題