开发者

HTMLPurifier iFrame / regex issue

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));


Change the preFilter regex to:

return preg_replace("/iframe/", "img class=\"MyIframe\" ", preg_replace("/<\/iframe>/", "", $html));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜