无法使用php domdocument追加img元素

时间:2017-05-23 23:00:09

标签: php domdocument

尝试使用php DomDocument将图像元素附加到noscript元素时,我遇到了一个奇怪的问题。

如果我创建一个新的div节点,我可以毫无问题地将它附加到noscript元素,但只要尝试附加图像元素,脚本就会超时。

我做错了什么?

eska@DESKTOP-1NGBVMN:~$ sudo nginx;
sudo: nie udało się rozwiązać nazwy hosta DESKTOP-1NGBVMN
nginx: [emerg] bind() to 0.0.0.0:5555 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:5555 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:5555 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:5555 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:5555 failed (98: Address already in use)
nginx: [emerg] still could not bind()
eska@DESKTOP-1NGBVMN:~$

1 个答案:

答案 0 :(得分:0)

你会陷入递归循环中。这将帮助您可视化正在发生的事情。为清晰起见,我添加了缩进:

php > $html = '<!DOCTYPE html><html><head><title>Sample</title></head><body><img src="https://example.com/images/example.jpg"></body></html>';
php > 
php > $doc = new DOMDocument();
php > $doc->loadHTML($html);
php > 
php > $images = $doc->getElementsByTagName('img');
php > 
php > $count=0;
php > foreach ($images as $image) {
php {     $count++;
php {     if($count>4) {
php {         die('limit exceeded'); 
php {     }
php { 
php {     $src = $image->getAttribute('src');
php {     $noscript = $doc->createElement('noscript');
php { 
php {     //$node = $doc->createElement('div');
php {     $node = $doc->createElement('img'); //If a uncomment this line the script just times out
php { 
php {     $node->setAttribute('src', $src);
php { 
php {     $noscript->appendChild($node);
php { 
php {     $image->setAttribute('x-data-src', $src);
php {     $image->removeAttribute('src');
php {     $image->parentNode->appendChild($noscript);
php {     //$image->parentNode->appendChild($newImage);
php { 
php { }
limit exceeded
php > $body = $doc->saveHTML();
php > 
php > echo $body;
<!DOCTYPE html>
<html><head><title>Sample</title></head><body>
<img x-data-src="https://example.com/images/example.jpg">
<noscript>
    <img x-data-src="https://example.com/images/example.jpg">
        <noscript>
        <img x-data-src="https://example.com/images/example.jpg">
            <noscript>
            <img x-data-src="https://example.com/images/example.jpg">
                <noscript>
                <img src="https://example.com/images/example.jpg">
                </noscript>
            </noscript>
        </noscript>
    </noscript>
    </body></html>
php > 

引起递归的麻烦行是

$image->parentNode->appendChild($noscript);

如果你发表评论,那么递归就会消失。请注意,当它递归时,x-data-src将应用于除最后一个之外的所有。

我还没有弄清楚造成这种行为的原因,但希望能够将其可视化将有助于您进一步诊断它。

**更新

OP采取了这个并与它一起运行,并用他的解决方案完成了答案,如下所示。

问题实际上是getElementsByTagName返回LiveNodeList,因此将图像附加到文档会导致无限递归。

我通过首先收集一个简单数组中的所有图像标记来解决它

<?php

$html = '<!DOCTYPE html><html><head><title>Sample</title></head><body><img src="https://example.com/images/example.jpg"></body></html>';

$doc = new DOMDocument();
$doc->loadHTML($html);

$images = $doc->getElementsByTagName('img');

$normal_array = [];
foreach ($images as $image) {
    $normal_array[] = $image;
}


// Now we have all tags in a simple array NOT in a Live Node List

foreach ($normal_array as $image) {

    $src = $image->getAttribute('src');
    $noscript = $doc->createElement('noscript');

    $node = $doc->createElement('img'); //If a uncomment this line the script just times out
    $node->setAttribute('src', $src);

    $noscript->appendChild($node);

    $image->setAttribute('x-data-src', $src);
    $image->removeAttribute('src');
    $image->parentNode->appendChild($noscript);

    //$image->parentNode->appendChild($newImage);

}

$body = $doc->saveHTML();