0

我正在做一些类似在本地应用程序中发布功能的事情,它确实工作正常,但它缺乏验证,更不用说我所做的验证是一团糟。我正在使用 jQuery oEmbed。

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

有什么建议么?

4

1 回答 1

3

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

首先替换所有 < 和 > 用于 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";

结果:

guax@trantor:~$ php teste.php 
&lt;html&gt;<b>test</b><strong>teste</strong>&lt;/html&gt

我想知道是否有任何解决方案可以一次全部更换。但它有效。

于 2011-06-17T17:32:52.537 回答