【发布时间】:2011-06-17 09:05:21
【问题描述】:
我正在做一些类似在本地应用程序中发布功能的事情,它确实运行良好,但它缺乏验证,更不用说我所做的验证是一团糟。我正在使用 jQuery oEmbed。
我想要的是按原样打印非法的 html 标签并激活/执行(我不知道正确的术语)我允许的 html 标签。
有什么建议吗?
【问题讨论】:
标签: php jquery html strip-tags oembed
我正在做一些类似在本地应用程序中发布功能的事情,它确实运行良好,但它缺乏验证,更不用说我所做的验证是一团糟。我正在使用 jQuery oEmbed。
我想要的是按原样打印非法的 html 标签并激活/执行(我不知道正确的术语)我允许的 html 标签。
有什么建议吗?
【问题讨论】:
标签: php jquery html strip-tags oembed
这是我想出的最佳解决方案。
首先将所有 替换为 html 代码,然后替换回允许的标签。
<?php
$original_str = "<html><b>test</b><strong>teste</strong></html>";
$allowed_tags = array("b", "strong");
$sans_tags = str_replace(array("<", ">"), array("<",">"), $original_str);
$regex = sprintf("~<(/)?(%s)>~", implode("|",$allowed_tags));
$with_allowed = preg_replace($regex, "<\\1\\2>", $sans_tags);
echo $with_allowed;
echo "\n";
结果:
guax@trantor:~$ php teste.php
<html><b>test</b><strong>teste</strong></html>
我想知道是否有任何解决方案可以一次全部更换。但它有效。
【讨论】: