PHP DOMDocument不会删除所有元素

时间:2019-03-17 10:13:32

标签: php domdocument

我要删除所有script元素和此处的代码

<?php
$pageFile = <<<EOF
<!DOCTYPE html><html><body>
<script src="aa"></script>
<script src="bb"></script>
<script src="cc"></script>
<div>aaa</div>
</body></html>
EOF;

$dom = new DOMDocument();
$dom->loadHTML($pageFile);

foreach ($dom->getElementsByTagName('script') as $item) {
  $item->parentNode->removeChild($item);
}
$pageFile = $dom->saveHTML();
echo $pageFile;

,但是仍然存在1个script元素。您可以在线here

试用

结果:

<!DOCTYPE html>
<html><body>
<script src="bb"></script><div>aaa</div>
</body></html>

2 个答案:

答案 0 :(得分:2)

DOMNodeList返回的$dom->getElementsByTagName是“活动的”。因此,当您删除脚本时,会将其从节点列表中删除,并且列表中的所有元素都将其索引向下移动。然后for循环转到下一个索引,最终跳过所有其他元素。

首先将节点列表转换为数组。

foreach (iterator_to_array($dom->getElementsByTagName('script')) as $item) {
  $item->parentNode->removeChild($item);
}

答案 1 :(得分:1)

如果向后迭代,则可以从DOMNodeList修改甚至删除节点: http://php.net/manual/en/class.domnodelist.php#83390 使用:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PresenceChannel;

class ProfileInvited implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

/**
 * Create a new event instance.
 *
 * @return void
 */
    public $message;

    public function __construct($message)
    {
        $this->message = $message;
        $this->dontBroadcastToCurrentUser();
    }

/**
 * Get the channels the event should broadcast on.
 *
 * @return \Illuminate\Broadcasting\Channel|array
 */
    public function broadcastOn()
    {
        return new PrivateChannel('invitation.' . $this->message->to);
    }
}