I'm trying to scrape from some day by using CSS pseudo selector first-child.
$html = '
<div>
<section>
<section class="A">A</section>
<section class="B">B</section>
</section>
<section class="C">
C
</section>
<section>
<section class="D">D</section>
<section class="E">E</section>
</section>
</div>
';
$html = str_get_html($html);
$sections = $html->find('section:first-child');
foreach ($sections as $k => $e) {
echo '<div>' . $k . ' => ' . strip_tags($e) . '</div>';
}
}
What I expect:
0 => AB
1 => C
2 => DE
What I got? NOTHING!
By looking at documentation here
https://simplehtmldom.sourceforge.io/docs/1.9/api/simple_html_dom/firstChild/
you can find firstChild()
method so i made
~~~
$sections = $html->find('section').firstChild();
~~~
but I got
~~~
Fatal error: Uncaught Error: Call to undefined function firstChild() in XXX.php:142 Stack trace: #0 XXX.php(XX): function_name('$html') #1 {main} thrown in XXX.php on line XXX
~~~
So, how to get only the upper levels?
TNX. I love your PHP Simple HTML DOM Parser ❤️😘
Ok. Wrong test. The method it's called like
$sections = $html->find('section')->firstChild();
but I got the same an error because it's an Array.
So there's not the same as the CSS pseudo selector rule
:first-child
How to use it to get the same result as CSS?
TNX
Last edit: Hi Man O ManaO 2024-12-02