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

6

9

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');
Punt answered 8/5, 2015 at 5:34 Comment(4)
No, I'm not use jQueryPunt
I don't think there is a method for this but you can try $text = $crawler->filter('.coucu')->first()->extract(array('_text')); i believe it will return the same result but still worth a shotNervine
I have used extract function(). But it's not work.Punt
I guess that strip_tags_content is from gist.github.com/marcanuy/7651298. I personally don't like regexes for HTML, they lead to bad stuff (#591247).Freddiefreddy
M
3

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

$html = $crawler->filter('.coucu')->html();
$html = explode("<span", $html);
echo trim($html[0]);
Mauriciomaurie answered 26/5, 2015 at 14:36 Comment(0)
R
2

Based on the criteria within your question, I think you would be best served by modifying your CSS Selector to: $crawler = $crawler->filter('div.coucu > span')

From there you can go $span_text = $crawler->text();

or to simplify things: $text = $crawler->filter('div.coucu > span')->text();

The text() method returns the value of the first item within the list.

Relish answered 18/5, 2015 at 1:29 Comment(1)
I want to get "Get Description Coucu".Punt
C
2
function extractCurrentText(Crawler $crawler)
{
  $clone = new Crawler();
  $clone->addHTMLContent("<body><div>" . $crawler->html() . "</div></body>", "UTF-8");
  $clone->filter("div")->children()->each(function(Crawler $child) {
    $node = $child->getNode(0);
    $node->parentNode->removeChild($node);
  });
  return $clone->text();
}
Canine answered 22/2, 2019 at 5:59 Comment(0)
O
1

This works nicely without hacky workarounds:

$crawler->filter('.coucu')->children()->each(function (Crawler $crawler) {
    $crawler->getNode(0)->parentNode->removeChild($crawler->getNode(0));
});
$crawler->text(); // Get Description
Ocelot answered 21/11, 2019 at 14:18 Comment(1)
Get to the top! 👆Epistyle
F
0

The HTML-removing solution it's based on regexes to strip HTML away (bad idea Using regular expressions to parse HTML: why not?), and the explode solution is limited.

I came up going by difference: get all the text, then remove the non-own text with str_replace.

Freddiefreddy answered 6/4, 2018 at 15:17 Comment(0)
V
0
$div = $crawler->filter('.coucu')->html();
$span = $crawler->filter('.coucu > span')->html();
$text = strip_tags(str_replace($span,'',$div));
Vanburen answered 1/1, 2020 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.