Question

here is my html :

    <div id="main">

    <div id="child1">
    child1
<a href="#"> link1</a>
    </div>

    <div id="child2">
    child2
<a href="#"> link2 </a>
    </div>

    </div>

I am trying to return (echo in php) child1 and child2 as links this is part of a HUGE file so I need to loop through it. this is what I have so far but its not working :

$linkObjs = $html->find('#main');

foreach ($linkObjs as $linkObj) {
    $title = trim($linkObj->fildchild()->plaintext);
    $link  = trim($linkObj->fildchild()->href);


    echo '<p class="titro" ><a href="' . $link . '" >' . $title . '</a></p>';


}
Was it helpful?

Solution

Not sure exactly which part of the elements you needed so here's everything dissected.

//  Find all divs in #main
foreach ($html -> find('#main div') as $div)
{
    //  Find plain text in div
    foreach ($div -> find('text') as $text)
    {
        echo $text;
    }

    //  Find <a> tags and href
    foreach ($div -> find('a') as $a)
    {
        echo $a -> href;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top