php getElementsByTagName with specific attribute

后端 未结 2 1265
天命终不由人
天命终不由人 2020-12-12 08:10

I think this gets the first element called

$gallery = $objDOM->getElementsByTagName(\'gallery\')->item(0);
相关标签:
2条回答
  • 2020-12-12 08:43

    This is only possible with DOMXPath, e.g.

    $xp    = new DOMXPath($yourDOMDocument);
    $nodes = $xp->query('//gallery[@name="Third"]');
    

    or by iterating over the node list after the call to getElementsByTagName with

    foreach ($objDOM->getElementsByTagName('gallery') as $gallery) {
        if($gallery->getAttribute('name') === 'Third') {
             // do something
        }
    }
    
    0 讨论(0)
  • 2020-12-12 08:57

    As the name suggests getElementsByTagName() only accepts tag names. Try XPath instead

    $xpath = new DOMXPath ($objDOM);
    $nodeList = $xpath->query('gallery[@name="Third"]');
    $gallery = $nodeList->item(0);
    

    Dont tested it, so there may be errors, typos or something.

    0 讨论(0)
提交回复
热议问题