Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php DOMDocument和delete父标记_Php_Parsing_Domdocument - Fatal编程技术网

Php DOMDocument和delete父标记

Php DOMDocument和delete父标记,php,parsing,domdocument,Php,Parsing,Domdocument,我们通过url加载html。然后创建文档 libxml_use_internal_errors(true); // disable errors $oHtml = new DOMDocument(); if (!$oHtml->loadHTML($this->getHtml($aData['href']))) { return false; } 下一步是删除fancybox或其他弹出链接。。。在我们的例子中,图像代码是 <a onclick="return hs.

我们通过url加载html。然后创建文档

libxml_use_internal_errors(true); // disable errors

$oHtml = new DOMDocument();

if (!$oHtml->loadHTML($this->getHtml($aData['href']))) {
    return false;
}
下一步是删除fancybox或其他弹出链接。。。在我们的例子中,图像代码是

<a onclick="return hs.expand(this)" href="http://domain.com/uploads/09072014106.jpg">
    <img title="Some title" alt="Some title" src="http://domain.com/uploads/thumbs/09072014106.jpg">
</a>

我们为它执行我们的方法

$this->clearPopUpLink($oHtml); // delete parent <a tag....
$this->clearPopUpLink($oHtml);//删除父项
$oImg=$oLink->firstChild;
$oImg->setAttribute('src',$oLink->getAttribute('href');//设置img正确的src
//$oLink->parentNode->removeChild($oLink);
//$oLink->parentNode->replaceChild($oImg,$oLink);
$oLink->parentNode->insertBefore($oImg);//更换!?!?!?!
//echo$oHtml->ownerDocument->saveHtml($oImg);
}
}
}
现在问题。。。这个代码正在工作,但我不明白为什么!为什么clearPopUpLink()处理完所有“图像”后,它没有带标记的旧代码?我尝试使用(在第一次开始调查时)->insertBefore(),然后使用->removeChild()。首先是在当前图像之前添加简单(编辑过的)图像(使用
),然后删除旧节点图像(使用
)。但是它不起作用,它只是在每一秒钟(每一秒钟都正确地完成了)


所以,让我问一个简单的问题,如何以正确的方式做这件事?因为我认为下面的代码(clearPopUpLink)不够正确。。。请建议您的解决方案。

嗯,我将使用受信者XPath进行此操作,并确保锚被移除;您所展示的代码并没有明确说明这一点(我还没有测试它)


这意味着放下锚,但保留图像?是的,我需要删除父
标记,但
应保留在代码中(在相同的位置)。
private function clearPopUpLink($oHtml)
    {
        $aLink = $oHtml->getElementsByTagName('a');
        if (!$aLink->length) {
            return false;
        }

        for ($k = 0; $k < $aLink->length; $k++) {
            $oLink = $aLink->item($k);

            if (strpos($oLink->getAttribute('onclick'), 'return hs.expand(this)') !== false) {
//              <a onclick="return hs.expand(this)" href="http://domain.com/uploads/posts/2014-07/1405107411_09072014106.jpg">
//                  <img title="Some title" alt="Some title" src="http://domain.com/uploads/posts/2014-07/thumbs/1405107411_09072014106.jpg">
//              </a>
                $oImg = $oLink->firstChild;
                $oImg->setAttribute('src', $oLink->getAttribute('href')); // set img proper src

//                $oLink->parentNode->removeChild($oLink);
//                $oLink->parentNode->replaceChild($oImg, $oLink);
                $oLink->parentNode->insertBefore($oImg); // replacing!?!?!?!

//                echo $oHtml->ownerDocument->saveHtml($oImg);
            }
        }
    }
$xpath = new DOMXPath($doc);

foreach ($xpath->query('//a[contains(@onclick, "return hs.expand(this)")]/img') as $img) {
        $anchor = $img->parentNode;

        $anchor->parentNode->insertBefore($img, $anchor); // take image out
        $anchor->parentNode->removeChild($anchor); // remove empty anchor
}

echo $doc->saveHTML();