繁体   English   中英

如何从用户输入中删除不需要的 HTML 标签,但使用 DOMDocument 将文本保留在 PHP 中的标签内

[英]How to remove unwanted HTML tags from user input but keep text inside the tags in PHP using DOMDocument

我在 S3 中存储了大约 200 万个包含各种 HTML 的 HTML 页面。 我试图仅从这些存储的页面中提取内容,但我希望保留具有某些约束的 HTML 结构。 这个 HTML 都是用户提供的输入,应该被认为是不安全的。 因此,出于显示目的,我只想保留一些对属性和属性值有约束的 HTML 标签,但仍然保留所有正确编码的文本内容,即使是不允许的标签。

例如,我只想允许特定的标签,如<p><h1><h2><h3><ul><ol><li>等。但我也想保留在不允许的标签之间找到任何文本并保持其结构。 我还希望能够限制每个标签中的属性或强制将某些属性应用于特定标签。

例如,在下面的 HTML...

<div id="content">
  Some text...
  <p class="someclass">Hello <span style="color: purple;">PHP</span>!</p>
</div>

我希望结果是...

  Some text...
  <p>Hello PHP!</p>

因此去除不需要的<div><span>标签,所有标签的不需要的属性,并仍然保留<div><span>内的文本。

简单地使用strip_tags()在这里不起作用。 所以我尝试使用DOMDocuemnt执行以下操作

$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

foreach($dom->childNodes as $node) {
    if ($node->nodeName != "p") { // only allow paragraph tags
        $text = $node->nodeValue;
        $node->parentNode->nodeValue .= $text;
        $node->parentNode->removeChild($node);
    }
}

echo $dom->saveHTML();

这适用于没有嵌套标签的简单情况,但在 HTML 复杂时显然会失败。

我不能准确地在每个节点的子节点上递归调用这个函数,因为如果我删除该节点,我将失去所有进一步嵌套的子节点。 即使我将节点删除推迟到递归之后,文本插入的顺序也变得棘手。 因为我尝试深入并返回所有有效节点,然后开始将无效子节点的值连接在一起,结果非常混乱。

例如,假设我想在以下 HTML 中允许<p><em>

<p>Hello <strong>there <em>PHP</em>!</strong></p>

但我不想允许<strong> 如果<strong>嵌套了<em>我的方法就会变得非常混乱。 因为我会得到类似...

<p>Hello there !<em>PHP</em></p>

这显然是错误的。 我意识到获取整个nodeValue是一种糟糕的方法。 因此,我开始研究其他方法来一次一个节点地遍历整个树。 只是发现很难概括这个解决方案,以便它每次都能正常工作。

更新

使用strip_tags()解决方案或此处提供的答案对我的用例没有帮助,因为前者不允许我控制属性,而后者删除任何具有属性的标签。 我不想删除任何具有属性的标签。 我想明确允许某些标签,但仍然可以扩展控制可以在 HTML 中保留/修改哪些属性。

似乎这个问题需要分解成两个更小的步骤才能概括解决方案。

一、遍历DOM树

为了得到一个可行的解决方案,我发现我需要有一种合理的方法来遍历 DOM 树中的每个节点并检查它以确定它是否应该保持原样或修改。

所以我使用了以下方法作为从DOMDocument扩展的简单生成器。

class HTMLFixer extends DOMDocument {
    public function walk(DOMNode $node, $skipParent = false) {
        if (!$skipParent) {
            yield $node;
        }
        if ($node->hasChildNodes()) {
            foreach ($node->childNodes as $n) {
                yield from $this->walk($n);
            }
        }
    }
}

通过这种方式执行诸如foreach($dom->walk($dom) as $node)事情给了我一个简单的循环来遍历整个树。 当然,这是一个 PHP 7 唯一的解决方案,因为语法的yield from ,但我对此表示同意。

二、删除标签但保留其文本

棘手的部分是弄清楚如何在循环内进行修改时保留文本而不是标签。 因此,在尝试了几种不同的方法后,我发现最简单的方法是构建一个要从循环内部删除的标签列表,然后稍后使用DOMNode::insertBefore()将文本节点附加到树上。 这样以后删除这些节点没有副作用。

所以我为DOMDocument这个子类添加了另一个通用的stripTags方法。

public function stripTags(DOMNode $node) {
    $change = $remove = [];
    
    /* Walk the entire tree to build a list of things that need removed */
    foreach($this->walk($node) as $n) {
        if ($n instanceof DOMText || $n instanceof DOMDocument) {
            continue;
        }
        $this->stripAttributes($n); // strips all node attributes not allowed
        $this->forceAttributes($n); // forces any required attributes
        if (!in_array($n->nodeName, $this->allowedTags, true)) {
            // track the disallowed node for removal
            $remove[] = $n;
            // we take all of its child nodes for modification later
            foreach($n->childNodes as $child) {
                $change[] = [$child, $n];
            }
        }
    }
    
    /* Go through the list of changes first so we don't break the
       referential integrity of the tree */
    foreach($change as list($a, $b)) {
        $b->parentNode->insertBefore($a, $b);
    }

    /* Now we can safely remove the old nodes */
    foreach($remove as $a) {
        if ($a->parentNode) {
            $a->parentNode->removeChild($a);
        }
    }
}

这里的技巧是因为我们在不允许的标签的子节点(即文本节点)上使用insertBefore将它们向上移动到父标签,我们可以很容易地破坏树(我们正在复制)。 起初这让我很困惑,但看看该方法的工作方式,这是有道理的。 例如,当更深的节点是允许的,但其父节点不在允许的标签列表中时,推迟节点的移动可确保我们不会破坏parentNode引用。

完整的解决方案

这是我想出的完整解决方案,以更普遍地解决这个问题。 我将包括在我的答案中,因为我在其他地方使用 DOMDocument 很难找到很多边缘情况。 它允许您指定允许使用哪些标签,并删除所有其他标签。 它还允许您指定允许哪些属性以及可以删除所有其他属性(甚至强制某些标签上的某些属性)。

class HTMLFixer extends DOMDocument {
    protected static $defaultAllowedTags = [
        'p',
        'h1',
        'h2',
        'h3',
        'h4',
        'h5',
        'h6',
        'pre',
        'code',
        'blockquote',
        'q',
        'strong',
        'em',
        'del',
        'img',
        'a',
        'table',
        'thead',
        'tbody',
        'tfoot',
        'tr',
        'th',
        'td',
        'ul',
        'ol',
        'li',
    ];
    protected static $defaultAllowedAttributes = [
        'a'   => ['href'],
        'img' => ['src'],
        'pre' => ['class'],
    ];
    protected static $defaultForceAttributes = [
        'a' => ['target' => '_blank'],
    ];

    protected $allowedTags       = [];
    protected $allowedAttributes = [];
    protected $forceAttributes   = [];

    public function __construct($version = null, $encoding = null, $allowedTags = [],
                                $allowedAttributes = [], $forceAttributes = []) {
        $this->setAllowedTags($allowedTags ?: static::$defaultAllowedTags);
        $this->setAllowedAttributes($allowedAttributes ?: static::$defaultAllowedAttributes);
        $this->setForceAttributes($forceAttributes ?: static::$defaultForceAttributes);
        parent::__construct($version, $encoding);
    }

    public function setAllowedTags(Array $tags) {
        $this->allowedTags = $tags;
    }

    public function setAllowedAttributes(Array $attributes) {
        $this->allowedAttributes = $attributes;
    }

    public function setForceAttributes(Array $attributes) {
        $this->forceAttributes = $attributes;
    }

    public function getAllowedTags() {
        return $this->allowedTags;
    }

    public function getAllowedAttributes() {
        return $this->allowedAttributes;
    }

    public function getForceAttributes() {
        return $this->forceAttributes;
    }

    public function saveHTML(DOMNode $node = null) {
        if (!$node) {
            $node = $this;
        }
        $this->stripTags($node);
        return parent::saveHTML($node);
    }

    protected function stripTags(DOMNode $node) {
        $change = $remove = [];
        foreach($this->walk($node) as $n) {
            if ($n instanceof DOMText || $n instanceof DOMDocument) {
                continue;
            }
            $this->stripAttributes($n);
            $this->forceAttributes($n);
            if (!in_array($n->nodeName, $this->allowedTags, true)) {
                $remove[] = $n;
                foreach($n->childNodes as $child) {
                    $change[] = [$child, $n];
                }
            }
        }
        foreach($change as list($a, $b)) {
            $b->parentNode->insertBefore($a, $b);
        }
        foreach($remove as $a) {
            if ($a->parentNode) {
                $a->parentNode->removeChild($a);
            }
        }
    }

    protected function stripAttributes(DOMNode $node) {
        $attributes = $node->attributes;
        $len = $attributes->length;
        for ($i = $len - 1; $i >= 0; $i--) {
            $attr = $attributes->item($i);
            if (!isset($this->allowedAttributes[$node->nodeName]) ||
                !in_array($attr->name, $this->allowedAttributes[$node->nodeName], true)) {
                $node->removeAttributeNode($attr);
            }
        }
    }

    protected function forceAttributes(DOMNode $node) {
        if (isset($this->forceAttributes[$node->nodeName])) {
            foreach ($this->forceAttributes[$node->nodeName] as $attribute => $value) {
                $node->setAttribute($attribute, $value);
            }
        }
    }

    protected function walk(DOMNode $node, $skipParent = false) {
        if (!$skipParent) {
            yield $node;
        }
        if ($node->hasChildNodes()) {
            foreach ($node->childNodes as $n) {
                yield from $this->walk($n);
            }
        }
    }
}

所以如果我们有以下 HTML

<div id="content">
  Some text...
  <p class="someclass">Hello <span style="color: purple;">P<em>H</em>P</span>!</p>
</div>

我们只想允许<p><em>

$html = <<<'HTML'
    <div id="content">
      Some text...
      <p class="someclass">Hello <span style="color: purple;">P<em>H</em>P</span>!</p>
    </div>
HTML;

$dom = new HTMLFixer(null, null, ['p', 'em']);
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

echo $dom->saveHTML($dom);

我们会得到这样的东西......

Some text...
      <p>Hello P<em>H</em>P!</p>

由于您也可以将其限制为 DOM 中的特定子树,因此该解决方案可以进一步推广。

您可以像这样使用 strip_tags():

$html = '<div id="content">
  Some text...
  <p class="someclass">Hello <span style="color: purple;">PHP</span>!</p>
</div>';
$updatedHTML = strip_tags($text,"<p><h1><h2><h3><ul><ol><li>"); 
   //in second parameter we need to provide which html tag we need to retain.

您可以在此处获取更多信息: http : //php.net/manual/en/function.strip-tags.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM