2011-06-17 55 views
0

我正在做一些类似于在本地应用程序中发布功能的东西,它的工作很好,但它缺少验证,更不用说我做的验证是一团糟。我正在使用jQuery oEmbed。允许一些html标签的标签剥离 - Facebook-ish

我想要的是按照原样打印非法的html标签,并激活/执行(我不知道正确的术语)我允许的html标签。

有什么建议吗?

回答

3

这是我想出的最佳解决方案。

首先将所有的<替换为> for html代码,然后替换回允许的标签。

<?php 

$original_str = "<html><b>test</b><strong>teste</strong></html>"; 
$allowed_tags = array("b", "strong"); 

$sans_tags = str_replace(array("<", ">"), array("&lt;","&gt;"), $original_str); 

$regex = sprintf("~&lt;(/)?(%s)&gt;~", implode("|",$allowed_tags)); 
$with_allowed = preg_replace($regex, "<\\1\\2>", $sans_tags); 

echo $with_allowed; 
echo "\n"; 

结果:

[email protected]:~$ php teste.php 
&lt;html&gt;<b>test</b><strong>teste</strong>&lt;/html&gt 

我不知道是否有更换一次全部的任何解决方案。但它的工作。

+0

哦,我没有尝试过这一个人。但无论如何,荣誉!我会测试你的脚本。 BTY! 谢谢你! – easyb 2011-06-20 05:57:11

+0

+1哇,非常感谢!这是一段非常棒的代码,我一直在寻找这么久。它很棒! – Nathan 2012-03-01 00:02:12