1

The example is pretty simple. I am trying to experiment with the Symfony's DomCrawler and trying to get the download section of the official PHP page https://www.php.net/ :

    <?php
    
    require __DIR__.'/../vendor/autoload.php';
    use Symfony\Component\DomCrawler\Crawler;
    use Masterminds\HTML5;
    
    $url = 'https://www.php.net/';
    $content = file_get_contents($url);
    
    
    $crawler = new Crawler($content);
    $item = $crawler->filterXPath("/html/body/div[2]/div/div/div[2]");
    dd($item);

But dd($item) returns the same as dd($crawler), as if the filterXpath() didn't make anything. Am i missing something? I would expect $item to have something like "'< div >divcontent< /div >'".

$item

Kos-Mos
  • 415
  • 1
  • 5
  • 15
  • the return value of `filterXPath` is essentially another crawler that has most of the same properties (especially $document is the same), but differs in: $nodes and you should really iterate over the result to get the nodes you matched. essentially all filter/search functions will call [`createSubCrawler`](https://github.com/symfony/symfony/blob/5.0/src/Symfony/Component/DomCrawler/Crawler.php#L1207) to allow accessing parents and stuff. but will mostly differ in nodes only (as seen in that function) – Jakumi Oct 19 '20 at 18:21
  • I am not sure if understood what you said. How should i get the current node string? Also in the example, nodes: [] in the $item variable. – Kos-Mos Oct 19 '20 at 18:48
  • In fact, if i try any method after filterXPath, it will return "Uncaught InvalidArgumentException: The current node list is empty." which confirms my suspiction that filterXpath isn't doing anything. – Kos-Mos Oct 19 '20 at 19:02
  • it's doing something, but apparently, nothing is matched, so your node list is empty. I just tried your script, and apparently you can't match the `` tag via `/html` but instead have to do `//html`. with that change, your node list should not be empty anymore – Jakumi Oct 19 '20 at 19:57
  • Yes, you're correct. using //htm instead of /html solves the problem. tahnk you so much for your good will to help, Jakumi! Still find a bti weird that it only works using //html instead of the official xpath provided by the browser (/html...). Maybe it could be considered a bug? – Kos-Mos Oct 19 '20 at 21:07

0 Answers0