在节点html中查找元素

时间:2017-05-22 08:53:54

标签: php html dom xpath

我不知道在我的代码中我做错了什么我想循环所有p标签并找到图像并用图标签包装

这是我的html示例:

$html = <<<EOF
<p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</p>
<p>
    <img src="xxxxx1" />
    <span class="sourceimgtest">AAAAA</span>
</p>

<p>
    <img src="xxxxx2">
</p>
<p>
    <img src="xxxxx3">
</p>
<p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</p>
<p>
    <img src="xxxxx4">
    <span class="sourceimgtest">BBBBB</span>
</p>
<p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</p>
<p>
    <img src="xxxxx5">
    <span class="sourceimgtest">CCCCC</span>
</p>
<p>
    <img src="xxxxx6">
    <span class="sourceimgtest">DDDDD</span>

    <img src="xxxxx7">
    <span class="sourceimgtest">EEEEE</span>

    <img src="xxxxx8">
    <span class="sourceimgtest">FFFFF</span>

</p>
EOF;

预期的产出:

<p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</p>
<p>
    <figure>
    <img src="xxxxx1" />
    </figure>
    <span class="sourceimgtest">AAAAA</span>
</p>

<p>
    <figure>
    <img src="xxxxx2">
    </figure>
</p>
<p>
    <figure>
    <img src="xxxxx3">
    </figure>
</p>
<p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</p>
<p>
    <figure>
    <img src="xxxxx4">
    </figure>
    <span class="sourceimgtest">BBBBB</span>
</p>
<p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</p>
<p>
    <figure>
    <img src="xxxxx5">
    </figure>
    <span class="sourceimgtest">CCCCC</span>
</p>
<p>
    <figure>
    <img src="xxxxx6">
    </figure>
    <span class="sourceimgtest">DDDDD</span>

    <figure>
    <img src="xxxxx7">
    </figure>
    <span class="sourceimgtest">EEEEE</span>

    <figure>
    <img src="xxxxx8">
    </figure>
    <span class="sourceimgtest">FFFFF</span>

</p>

这是我正在使用的php:

<?php
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);


$matches = $xpath->query('//p');


if($matches->length > 0 ){


    foreach($matches as $node){

    $node_img_match = $node->getElementsByTagName('img');
    if(isset($node_img_match)){
        foreach($node_img_match as $node_img ){

            $figure_node = $dom->createElement('figure');
            $figure_node->appendChild($node_img);  
            $node->parentNode->replaceChild($figure_node, $node);

        }
    }
}

$contenu = $dom->saveHTML();
echo $contenu;

当我执行它时,我有这个错误:

Fatal error: Uncaught Error: Call to a member function replaceChild() on null 

2 个答案:

答案 0 :(得分:1)

我看到$node是您的<p>标记,因此无需获取此标记的父级。您应该完全替换$node。但是,您还应该获得<img>代码的副本,并将其替换为$node_img->cloneNode()

试试这个

foreach ($node_img_match as $node_img) {
   $figure_node = $dom->createElement('figure');
   $figure_node->appendChild($node_img->cloneNode());
   $node->replaceChild($figure_node,$node_img);
}

答案 1 :(得分:0)

此行中的$node

$node->parentNode->replaceChild($figure_node, $node);

如果我没有弄错,则引用<p>元素,因此parentNode会根据您的HTML引用null元素。

这只是一个疯狂的猜测,但你可以这样说:

$node->replaceChild($figure_node, $node_img_match);

$node_img_match->parentNode->replaceChild($figure_node, $node_img_match);