简体   繁体   English

PHP appendChild给人致命的错误-消息“层次结构请求错误”的未捕获异常“ DOMException”-如何在标记后添加html

[英]PHP appendChild giving nice fatal error -Uncaught exception 'DOMException' with message 'Hierarchy Request Error - how can I add html AFTER a tag

In my piece of code I'm trying to find all img tags using PHP DOM, add another img tag directly after and then wrap all of that in a div, ie 在我的代码段中,我试图使用PHP DOM查找所有img标签,然后在其后直接添加另一个img标签,然后将所有这些标签包装在div中,即

<!-- From this... -->
<img src="originalImage.jpg" />

<!-- ...to this... -->
<div class="wrappingDiv">
    <img src="originalImage.jpg" />
    <img src="newImage.jpg" />
</div>

This is the PHP that I'm bastardising trying: 这是我 bastardising 尝试PHP:

$dom = new domDocument;
$dom->loadHTML($the_content_string);
$dom->preserveWhiteSpace = false;
    //get all images and chuck them in an array
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
            //create the surrounding div
    $div = $image->ownerDocument->createElement('div');
    $image->setAttribute('class','main-image');
        $added_a = $image->parentNode->insertBefore($div,$image);
        $added_a->setAttribute('class','theme-one');
        $added_a->appendChild($image);
            //create the second image
    $secondary_image = $image->ownerDocument->createElement('img');
    $added_img = $image->appendChild($secondary_image);
    $added_img->setAttribute('src', $twine_img_url);
    $added_img->setAttribute('class', $twine_class);
    $added_img->appendChild($image);
    }

echo $dom->saveHTML();

Everything up to where I create the $added_img variable works fine. 直到我创建$ added_img变量的所有地方都可以正常工作。 Well, at the very least it doesn't error. 好吧,至少它不会出错。 It's those last four lines that kill it dead. 是最后四行杀死了它。

I'm clearly doing something relatively idiotic... Any lovely, lovely people out there able to point out where I've douched things up? 我显然在做一些相对愚蠢的事情……有任何可爱,可爱的人能够指出我在哪里做过什么?

First: 第一:

You are trying to append and image to an image here(but of course in HTML an image cannot have an child-image, append the images to the div): 您正在尝试在此处将图像附加到图像(但是在HTML中,图像当然不能具有子图像,请将图像附加到div):

$added_img = $image->appendChild($secondary_image);

it has to be 它一定要是

$added_img = $added_a->appendChild($secondary_image);

and again here: 再次在这里:

$added_img->appendChild($image);

has to be: 必须:

$added_a->appendChild($image);

But this will not work at all, because NodeList's are live. 但这根本不起作用,因为NodeList处于活动状态。 As soon as you append one new image, this image is a part of $images, you will run into an infinite loop. 追加一个新图像后,该图像即成为$ images的一部分,您将陷入无限循环。 So first populate an array with the initial images instead of using a NodeList. 因此,首先使用初始图像填充数组,而不使用NodeList。

$imageList= $dom->getElementsByTagName('img');
$images=array();
for($i=0;$i<$imageList->length;++$i)
{
  $images[]=$imageList->item($i);
}

暂无
暂无

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

相关问题 带有消息'Hierarchy Request Error'的未捕获异常'DOMException' - Uncaught exception 'DOMException' with message 'Hierarchy Request Error' PHP致命错误:带有消息的未捕获异常“异常” - PHP fatal error: Uncaught exception 'Exception' with message PHP XML DOM未捕获的异常“ DOMException”,消息为“错误的文档错误” - PHP XML DOM Uncaught exception 'DOMException' with message 'Wrong Document Error' 未捕获的异常&#39;DOMException&#39;,消息&#39;Not Found Error&#39; - Uncaught exception 'DOMException' with message 'Not Found Error' PHP致命错误:消息为&#39;SQLSTATE [42000]的未捕获异常&#39;PDOException&#39; - PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] Phalcon PHP致命错误:消息“未知表达式”未捕获的异常 - Phalcon PHP Fatal Error: Uncaught exception with message 'Unknown Expression' PHP致命错误:消息为&#39;SQLSTATE [42000]的未捕获异常&#39;PDOException&#39;: - PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: 致命错误:未捕获的DOMException:无效字符错误 - Fatal error: Uncaught DOMException: Invalid Character Error 致命错误:消息为“ 1048”的未捕获异常“ Exception” - Fatal error: Uncaught exception 'Exception' with message '1048 致命错误:消息为“ DateTimeZone ::?”的未捕获异常“ Exception” - Fatal error: Uncaught exception 'Exception' with message 'DateTimeZone::?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM