【问题标题】:Filter URL of img tags with HTMLpurifier使用 HTMLpurifier 过滤 img 标签的 URL
【发布时间】:2019-10-02 11:55:18
【问题描述】:

我正在使用 htmlPurifier 过滤用户生成的 html。我想使用特定规则集过滤所有图像 URL,同时允许更通用的超链接。

如何设置仅适用于 img.src 属性而不适用于 a.href 属性的 URL 过滤器?

我想,我需要设置一个 URI 过滤器,如 http://htmlpurifier.org/docs/enduser-uri-filter.html 中所述。但我需要以某种方式将其限制为 img.src 属性。

如果我写这样的过滤器,有没有办法确定我当前是否正在对 img.src 属性进行操作?

class HTMLPurifier_URIFilter_NameOfFilter extends HTMLPurifier_URIFilter
{
    public $name = 'NameOfFilter';
    public function prepare($config) {}
    public function filter(&$uri, $config, $context) {}
}

【问题讨论】:

    标签: php htmlpurifier


    【解决方案1】:

    我刚刚找到了解决方案: $context 对象包含有关当前正在处理的标签的信息。因此,可以使用:

    class HTMLPurifier_URIFilter_MyImages extends HTMLPurifier_URIFilter {
        public $name = 'MyImages';
        public $post = true;
        public function prepare($config) {}
        public function filter(&$uri, $config, $context) {
            if ($context->get('CurrentToken')->name == 'img') {
                // do something with the image url here
            }
            return true;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-20
      • 2023-04-01
      • 1970-01-01
      • 2019-05-20
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多