PHP DOMDocument : how to select all links under a specific tag

后端 未结 3 1540
耶瑟儿~
耶瑟儿~ 2021-01-23 15:33

I\'m just getting started with using php DOMDocument and am having a little trouble. How would I select all link nodes under a specific node lets say

in jquery i could

3条回答
  •  孤独总比滥情好
    2021-01-23 16:07

    Get all h5 tags from it, and loop through each one, checking if it's parent is an a tag.

    // ...
    $h5s = $document->getElementsByTagName('h5');
    $correct_tags = array();
    foreach ($h5s as $h5) {
        if ($h5->parentNode->tagName == 'a') {
            $correct_tags[] = $h5;
        }
    }
    // do something with $correct_tags
    

提交回复
热议问题