removeNode无法正常工作

时间:2015-02-03 11:38:27

标签: php domdocument

我想删除所有以' /'开头的锚标记。这是我的代码:

$html = <<<HTML
<ul>
  <li><a href="/foo/bar1">link1</li>
  <li><a href="/foo/bar2">link2</li>
  <li><a href="/foo/bar3">link3</li>
</ul>
HTML;

$dom = new DOMDocument;
@$dom->loadHTML($html);
$tags = $dom->getElementsByTagName('a');
echo 'removed nodes:<br />';
foreach ($tags as $tag)
{
    $href = $tag->getAttribute('href');
    if($href[0] == '/')
    {
        echo $tag->nodeValue.'<br />';
        $tag->parentNode->removeChild($tag);
    }
}
echo 'remined content:<br />';
echo $dom->saveXML($dom);

但问题是它提醒了其中一些人。

removed nodes:<br>
link1<br>
link3<br>

remined content:<br>
<ul><li>
  </li><li><a href="/foo/bar2">link2</a></li>
  <li>
</li></ul>

关于如何做到这一点的任何想法? 感谢。

1 个答案:

答案 0 :(得分:1)

当您在foreach循环(http://php.net/manual/en/domnode.removechild.php#90292)中迭代DOMNode时,您无法从DOMNodeList中删除DOMNode。但是,要删除项目的队列似乎有效:

<?php
$html = <<<HTML
<ul>
  <li><a href="/foo/bar1">link1</a></li>
  <li><a href="/foo/bar2">link2</a></li>
  <li><a href="/foo/bar3">link3</a></li>
</ul>
HTML;

$dom = new DOMDocument;
@$dom->loadHTML($html);


$domNodeList = $dom->getElementsByTagName('a'); 
$domElemsToRemove = array(); 
foreach ($domNodeList as $domElement ) { 
  $domElemsToRemove[] = $domElement; 
} 
echo 'removed nodes:<br />';
foreach ($domElemsToRemove as $tag)
{
    $href = $tag->getAttribute('href');
    if($href[0] == '/')
    {
        echo $tag->nodeValue.'<br />';
        $tag->parentNode->removeChild($tag);
    }
}
echo 'remined content:<br />';
echo $dom->saveXML($dom);

修改

您也忘了关闭代码<a>