simplehtmldom return the content of 2 divs inside another
-
20-12-2019 - |
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>';
}
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