2013-04-17 45 views
0

正如标题所说,当我使用strip_tags($text, "<a>...<others>")和我插入超链接到$text,我留下的<a>而不是<a href='...'>输出。我该如何解决这个问题,或者是否对此不够灵活?用strip_tags是保持<a>但剥离其属性

谢谢!

$text = ucfirst($text); 
$text = preg_replace('/\v+|\\\r\\\n/', '<br />', $text); 
$allow = '<p><br><ul><ol><li><strong><img><em><a>'; 
$text = strip_tags($text, $allow); 

// now, remove any suspect tags, etc. 
preg_match_all("/<([^>]+)>/i", $allow, $allTags, PREG_PATTERN_ORDER); 

输入$text是:

<p>Lorum ipsum &nbsp;<a href="http://www.test.com">This is a test link</a>.</p> 

回答

0

strip_tags去除所有HTML元素或与<>封闭任何事情。

manual

用strip_tags - 地带HTML和PHP标签从字符串

实施例:

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>'; 

输出:

Test paragraph. Other text 
0

它应该像你想要的一样工作。

$text = '<a href="http://www.someplace.com">link</a> <others>stuff</others>'; 
echo strip_tags($text,"<a>"); 

我留下了完整的<a>标签:

<a href="http://www.someplace.com">link</a> stuff 

工作例如:http://3v4l.org/j1uE1

更新:随着您在更新的问题发布的代码,我跑了这里仍然提供完整的<a>标签输出。属性没有被剥离。

http://3v4l.org/tFQ4l