DOMDocument and delete parent tag

假装没事ソ 提交于 2020-01-05 12:35:15

问题


We load html by url. After that creating DOMDocument

libxml_use_internal_errors(true); // disable errors

$oHtml = new DOMDocument();

if (!$oHtml->loadHTML($this->getHtml($aData['href']))) {
    return false;
}

Next step is to delete fancybox or other popUp links... In our case image code is

<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>

And we execute our method for it ...

$this->clearPopUpLink($oHtml); // delete parent <a tag....

Method...

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);
            }
        }
    }

Now questions... This code is working BUT I don't get WHY! Why when clearPopUpLink() done with all "images" it has not OLD code with tags? I tried to use (in first time when start investigation) ->insertBefore(), after that ->removeChild(). First is add simple (edited) image BEFOR current image (with <a>), after that delete old node image (with <a>). BUT! It doesn't work, it was doing only on each second (each first was done correctly).

So, let me ask simple question, how to do it in right way? Because I don't think that code below (clearPopUpLink) is correct enough... Please suggest your solutions.


回答1:


Hmm, I would use the trustee XPath for this and make sure the anchor gets removed; the code you've shown doesn't exactly make that obvious (I haven't tested it).

$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();


来源:https://stackoverflow.com/questions/24713728/domdocument-and-delete-parent-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!