2013-10-17 30 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); 
相关问题