duan7264 2011-02-28 15:49
浏览 68
已采纳

HTMLPurifier iFrame / regex问题

I am using the code provided on HTMLPurifier forums to allow support for iFrame tags, as Google, YouTube and others are now using iframe instead of embed for videos and maps.

Here is the code:

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>';
    }
}

It almost works, except for one issue, this is the result:

<iframe height="275" src="omitted"></iframe> "class="MyIframe" >"

How can I get the class to be a part of the iframe tag?

UPDATE: Sorry, saw a matching question after posting.. initial search turned up nothing. Here is what had to change in the preFilter function:

return preg_replace("/iframe/", "img class=\"MyIframe\" ", preg_replace("/<\/iframe>/", "", $html));
  • 写回答

1条回答 默认 最新

  • dpict99695329 2011-02-28 15:59
    关注

    Change the preFilter regex to:

    return preg_replace("/iframe/", "img class=\"MyIframe\" ", preg_replace("/<\/iframe>/", "", $html));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错