DOMDocument并删除父标记 [英] DOMDocument and delete parent tag

查看:72
本文介绍了DOMDocument并删除父标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们通过url加载html.之后,创建DOMDocument

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

下一步是删除fancybox或其他弹出链接...在我们的示例中,图片代码为

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

方法...

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

现在的问题...该代码有效,但是我不明白为什么!为什么用clearPopUpLink()处理所有图像"时,它没有带有标签的OLD代码?我尝试使用(第一次开始调查时)-> insertBefore(),之后使用-> removeChild().首先是在当前图像之前(使用<a>)添加简单(已编辑)图像,之后再删除旧节点图像(使用<a>).但!它不起作用,仅每秒执行一次(每个步骤都正确完成了).

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

那么,让我问一个简单的问题,如何以正确的方式做到这一点?因为我认为下面的代码(clearPopUpLink)不够正确,请提出您的解决方案.

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.

推荐答案

嗯,我将为此使用受托人XPath并确保删除锚点;您显示的代码并不能使它变得显而易见(我尚未测试).

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

这篇关于DOMDocument并删除父标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆