Question

I am using Curl to grab the URL and SIMPLE HTML DOM to get the data. I want the data which is under <li>. But the problem is there are other <ul> and <li>'s on the page. And they don't have classes or id's. Here is my html code.

<div class="parent" id="parent">
    <div class="child">
        <div class="grandchild">
            <p>Text Paragraph</p>
            <h2>Heading</h2>
            <ul>
                <li>Heading Date Text</li>
                <li>Heading Date Text</li>
                <li>Heading Date Text</li>
                <li>Heading Date Text</li>
                <li>Heading Date Text</li>
            </ul>
            <p>Text</p>
        </div>
    </div>
</div>

Only the top div has an id which is unique.

Was it helpful?

Solution 2

This will give you the result.

$html = str_get_html('<div class="parent" id="parent">
<div class="child">
    <div class="grandchild">
        <p>Text Paragraph</p>
        <h2>Heading</h2>
        <ul>
            <li>Heading Date Text</li>
            <li>Heading Date Text</li>
            <li>Heading Date Text</li>
            <li>Heading Date Text</li>
            <li>Heading Date Text</li>
        </ul>
        <p>Text</p>
    </div>
</div>
</div>');

foreach($html->find('div#parent ul') as $ul) 
{
   foreach($ul->find('li') as $li) 
   {
        echo $li->plaintext."<br>";
   }
}

OTHER TIPS

You can retrieve the li items with:

foreach ($html->find("#parent li") as $li)
    echo $li->plaintext . "\n";

There is no need for double iteration over the ULs and then the LIs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top