Search code examples
css-selectorsdomcrawler

DomCrawler 2nd and etc child


I'm reworking my parsers from SimpleHTMLDom on DomCrawler and on some points dcumentation(as for me) not covering all the questions which might be from newbie devs. In SHD i'm wrote just like this for access to second child:

$value = $dom->find('.t18',0);
$result = $value->find('b',1);

but in DomCrawler i can't find any similar way of selecting exactly second child of element with t18 class. How can i do that?

code example from which i'm trying get data:

<div class="t18">
  <b>20(PLN)</b>
  <b>4.66 (EUR)</b>
<br>
  <b>20(EUR)</b>
  <b>85.87(PLN)</b>
<br>
</div>

Solution

  • You can use "eq" function to select the n'th child of a node.

    for your example, to select second b element inside first .t18 element:

    echo $domCrawler->filter(".t18")->eq(0)->filter("b")->eq(1)->text(); // will echo 4.66 (EUR)