Get Desc...">

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'); 
+6
source share
2 answers

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.

+2
source

In the same situation. I ended up with:

 $html = $crawler->filter('.coucu')->html(); $html = explode("<span", $html); echo trim($html[0]); 
+2
source

Source: https://habr.com/ru/post/986796/


All Articles