Search code examples
htmlregexxmlsymfonydomcrawler

Whats the most efficient/nicest way to extract a text value from a HTML tag using Symfony DOM Crawler?


Given the following HTML code snippet:

<div class="item">
  large
  <span class="some-class">size</span>
</div>

I'm looking for the best way to extract the string "large" using Symfony's Crawler.

$crawler = new Crawler($html);

Here I could use $crawler->html() then apply a regex search. Is there a better solution? Or how would you do it exactly?


Solution

  • I've just found a solution that looks the cleanest to me:

    $crawler = new Crawler($html);
    $result = $crawler->filterXPath('//text()')->text();