删除除<a> and convert reserved to entities, keeping </a> <a> in tact</a>以外的HTML代码

时间:2012-09-27 18:04:06

标签: php email html-entities strip-tags

使用PHP,如何从HTML字符串中删除除锚点(<a>)之外的所有标记,然后将所有保留字符转换为除<a>标记之外的HTML实体?

例如,我想转换它:

<p><a href="http://www.example.com/">Example.com</a> says that 5 < 6.</p>

进入这个:

<a href="http://www.example.com/">Example.com</a> says that 5 &lt; 6.

2 个答案:

答案 0 :(得分:0)

这个怎么样:

$str = '<p><a href="http://www.example.com/">Example.com</a> says that 5 < 6.</p>';
echo $str."\n";
$stripped = strip_tags($str, '<a>');
echo $stripped."\n";

产生输出:

[stou$ ~]$ php hmm.php 
<p><a href="http://www.example.com/">Example.com</a> says that 5 < 6.</p>
<a href="http://www.example.com/">Example.com</a> says that 5 < 6.
[stou$ ~]$

编辑抱歉错过了关于html实体的观点。

答案 1 :(得分:0)

我和HTML Purifier一起去了。 Click here for the demo which converts the original example正确。

执行此操作的代码是:

$config = HTMLPurifier_Config::createDefault();
$config->set('Core.Encoding', 'UTF-8');
$config->set('HTML.Allowed', 'a[href]');
$filter = new HTMLPurifier($config);
$output = $filter->purify($input);