开发者

PHP DOMDocument: How to get direct node childrens

开发者 https://www.devze.com 2023-01-10 01:21 出处:网络
I\'ve got the following: http://pastie.org/1066861 I\'ve got a Javascript function, that tells me to delete a given node from the parent, let\'s assume position 1 ( from 0 or 1, since we\'ve got two

I've got the following:

http://pastie.org/1066861

I've got a Javascript function, that tells me to delete a given node from the parent, let's assume position 1 ( from 0 or 1, since we've got two child nodes on parent ), When doing $parent->getElementsByTagName and trying to remove the the $child->item( 1 ), I notice that PHP GetElementsByTagName is giving me a diferent position, 开发者_如何学Pythonfor that specific node. So, there's no way to access the right node, that JS told.

I guess I should stay away of getElementsByTagName, and use getElementsById, but right now I would like to learn and understand why this doesnt work properly.

Here's some code: http://pastie.org/1066857

Thanks a lot for your atention ;D


Found a solution,

/*
 * deleteNode() removes given node, in position X.
 */
function deleteNode( $html, $strParent, $intNodePosition ) {

    $domhtml = DOMDocument::loadHTML($html); 

    $xpath = new DOMXPath($domhtml);

    $divs = $xpath->query( "*/div[@id='maindiv']/div" );

    $node = $divs->item( $intNodePosition );

    $node->parentNode->removeChild( $node );

    /*
    this wont work, but why ? It should
    $divs->removeChild( $node );
    */

return $domhtml->saveHTML();

}//fim deleteNode
0

精彩评论

暂无评论...
验证码 换一张
取 消