Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

New in Symfony 4.3: DomCrawler improvements

Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.

Extract Element Names

Contributed by
Andrey Helldar
in #29127.

The extract() method returns the attribute and/or node values from a given list of nodes. You can use the special attribute _text to get the value of nodes. In Symfony 4.3 you can also use the new special attribute _name to get the element name (the HTML tag name):

$attributes = $crawler->filterXPath('//body/*')
    ->extract(['_text', '_name', 'class'])

Default values for text() and html()

Contributed by
Roberto Espinoza
in #28581.

When a node is empty, calling the text() and html() methods resulted in an \InvalidArgumentException with the well-known message "The current node list is empty.". In Symfony 4.3 these methods allow passing an argument that will be returned when the node is empty, so you can consider it a "default" value for the node:

// return an empty string instead of throwing an exception
$title = $crawler->filter('aside h3')->text('');

// return a default HTML content instead of throwing an exception
$userProfile = $crawler->filter('.user-profile')->html('<b>Anonymous User</b>');
Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.


Good news
Nice improvements
Loved it!
good job!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.