Symfony 2 Dom Crawler: how to get only text () in an element
Using Crawler to get only text (no tag).
$html = EOT<<< <div class="coucu"> Get Description <span>Coucu</span> </div> EOT; $crawler = new Crawler($html); $crawler = $crawler->filter('.coucu')->first()->text(); Conclusion: Get a description of Coucu
I want to output (only): Get a description
UPDATE:
I found a solution for this: (but this is a really bad solution)
... $html = $crawler->filter('.coucu')->html(); // use strip_tags_content in https://php.net/strip_tags $html = strip_tags_content($html,'span'); Based on the criteria in your question, I think it's best to help you by changing your CSS selector to: $crawler = $crawler->filter('div.coucu > span')
From there you can go $span_text = $crawler->text();
or to simplify: $text = $crawler->filter('div.coucu > span')->text();
The text () method returns the value of the first element in the list.