2013-10-17 36 views
1

請建議我使用preg_replace的PHP正則表達式來移除HTML標記除<a>標記之外的所有屬性。php正則表達式去除除超鏈接之外的html標記的屬性<a>標籤

我已經試過:preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>',$htmltext);

它工作正常,所有的HTML標記但<a>標籤它消除HREF,標題和目標的屬性。

請建議我在上面的正則表達式中所需的更改或請分享工作。

在此先感謝。

+0

inb4'小子他來了'。 –

+0

@Ben Fortune - 那是什麼? – Hrishi

+0

http://stackoverflow.com/a/1732454/2615209 –

回答

3

to remove all the tags from HTML tags except <a> tag.

無需正則表達式,你可以使用strip_tags function

$html = strip_tags($html, '<a>'); 

UPDATE:的preg_replace只刪除全部來自HTML標籤的屬性除了從<a>。您可以使用這種基於負面lookahead的正則表達式:

$htmltext = preg_replace("~<(?!a\s)([a-z][a-z0-9]*)[^>]*?(/?)>~i",'<$1$2>', $htmltext); 
相關問題