Search code examples
phpdomcrawler

DOMCrawler truncates tags inside text()


I have a text with internal custom tags

$html = '<textarea name="main"><customtag>Hello World!</customtag></textarea>'

$crawler = new Crawler($html);
$text = $crawler->filter('textarea[name="main"]')->text();

print_r($text);

But DOMCrawler truncates these tags in the output

Hello World!

Why? And how to do that DOMCrawler leave custom tags?


Solution

  • In order to preserve child html tags then you need to us use the html() method like so:

    $html = $crawler->filter('textarea[name="main"]')->html();
    

    See https://symfony.com/doc/current/components/dom_crawler.html and http://api.symfony.com/4.0/Symfony/Component/DomCrawler/Crawler.html#method_html