2011-07-15 35 views
1

我有一段文字,其中包含我想要去除的特定圖像。問題是,該標籤可與不同風格的使用PHP從特定圖像的字符串中刪除IMG標記

爲如

<img src="myimage.png" alt="" class=""/> 

<img alt="" class="" src="myimage.png"/> 

<img class="" alt ="" src="myimage.png"/> 

現在我怎樣才能從刪除特定圖像標籤我的字符串使用PHP?

+0

可能重複(http://stackoverflow.com/questions/2772782/regex-remove-images-與-style-tag-from-html) – Gordon

+0

*(相關)* [最佳方法解析HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon

回答

1

喜歡的東西:

$str = 'Lorem <img alt="" class="" src="myimage.png"/> ipsum <img class="" alt="" src="myimage.png"/> dolor <img src="myimage.png"/> sit...'; 
echo preg_replace('!<img.*?src="myimage.png".*?/>!i', '', $str); 
// output: "Lorem ipsum dolor sit..." 

可能?

+0

工作。謝謝。 – Harry

0

如果你的意思是提取屬性,嘗試[正則表達式從HTML樣式標籤移除圖片]

$xpath = new DOMXPath(@DOMDocument::loadHTML($html)); 
$src = $xpath->evaluate("string(//img/@src)"); 
相關問題