我正在使用 HTMLPurifier 论坛上提供的代码来支持 iFrame 标签,因为 Google、YouTube 和其他人现在使用 iframe 而不是嵌入视频和地图。
这是代码:
class HTMLPurifier_Filter_MyIframe extends HTMLPurifier_Filter
{
public $name = 'MyIframe';
public function preFilter($html, $config, $context) {
return preg_replace("/iframe/", "img class=\"MyIframe\" ", $html);
}
public function postFilter($html, $config, $context) {
$post_regex = '#<img class="MyIframe" ([^>]+)>#';
return preg_replace_callback($post_regex, array($this, 'postFilterCallback'), $html);
}
protected function postFilterCallback($matches) {
return '<iframe '.$matches[1].'></iframe>';
}
}
它几乎可以工作,除了一个问题,结果如下:
<iframe height="275" src="omitted"></iframe> "class="MyIframe" >"
如何让类成为 iframe 标签的一部分?
更新:抱歉,发布后看到了一个匹配的问题.. 初始搜索什么也没找到。这是 preFilter 函数中必须更改的内容:
return preg_replace("/iframe/", "img class=\"MyIframe\" ", preg_replace("/<\/iframe>/", "", $html));