PHP DOM get items from first ul element

后端 未结 1 891
误落风尘
误落风尘 2021-01-14 05:15

I have html file which looks like this:

  • item 1
  • item 2
  • item 3
相关标签:
1条回答
  • 2021-01-14 05:40

    Passing a DomNode to foreach will not allow you to iterate through its childen, you can easily get the list of lis and iterate through them.

    <?php
        $dom = new DOMDocument();
        $dom->loadHTML($data);
        $postalCodesList = $dom->getElementsByTagName('ul');
        foreach ($postalCodesList->item(0)->getElementsByTagName('li') as $postalCodesList) {
            echo $postalCodesList->nodeValue.'<br />';                    
        }
    ?>
    

    DEMO

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