2012-07-08 47 views
0

我使用:刪除與preg_replace函數<a>聯繫,但保持<img>圖像內

$this->tresc[$i][description]=preg_replace("/\<a .*\>.*\<\/a\>/i", "", $this->tresc[$i][description]); 

刪除鏈接。

有時鏈接裏面有我的圖像想保留:

<a href="http://www.domain.com/page.php"><img src="http://domain.com/image.jpg" alt="​Image" align="left" /></a> 

這可能嗎?現在刪除了所有在<a></a>之間的內容。

+3

[不要使用正則表達式來解析HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – 2012-07-08 14:09:04

+0

I解析RSS提要。 – Spacedust 2012-07-08 14:09:55

+3

猜猜它是什麼。 – Ryan 2012-07-08 14:10:09

回答

2

我這樣做:

$this->tresc[$i][description]=preg_replace("/<a href=\"(.*)\">/i", "",$this->tresc[$i][description]); 
$this->tresc[$i][description]=preg_replace("/<\/a\>/i", "",$this->tresc[$i][description]); 

但是,這是留下的文字是這樣的鏈接。

7

PHP的strip_tags()函數允許您指定要保持不變的HTML實體。

http://php.net/manual/en/function.strip-tags.php

您可以使用可選的第二個參數來指定要 無法剝離的標籤。

strip_tags($rssContent, '<img>'); 

這應該刪除/消毒所有的HTML元素裏只剩下了<img>標籤。


PHP文檔中該頁面的註釋部分還包含大量有用的功能,這些功能可能對您有用。我建議通讀他們。
This one特別看起來很有趣。

+0

我會檢查出來。 – Spacedust 2012-07-08 14:13:33