Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 Dom Crawler: how to get only text() in Element

Using Dom Crawler to get only text (without tag).

$html = EOT<<<
  <div class="coucu">
    Get Description <span>Coucu</span>
  </div>
EOT;

$crawler = new Crawler($html);
$crawler = $crawler->filter('.coucu')->first()->text();

output: Get Description Coucu

I want to output (only): Get Description

UPDATE:

I found a solution for this: (but it's really bad solution)

...
$html = $crawler->filter('.coucu')->html();
// use strip_tags_content in https://php.net/strip_tags
$html = strip_tags_content($html,'span');
like image 454
Tue Vo Avatar asked May 08 '15 05:05

Tue Vo


1 Answers

Ran into the same situation. I ended up going with:

$html = $crawler->filter('.coucu')->html();
$html = explode("<span", $html);
echo trim($html[0]);
like image 120
wkm Avatar answered Oct 04 '22 19:10

wkm