Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4df912b

Browse files
feature #29127 [DomCrawler] Added return of element name in extract() method (andrey-helldar)
This PR was squashed before being merged into the 4.3-dev branch (closes #29127). Discussion ---------- [DomCrawler] Added return of element name in `extract()` method | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | License | MIT | Doc PR | symfony/symfony-docs#10646 Situation: you need to get an array of keys and values. The current package code does not allow this to be done easily. The changes made to the code will allow you to return the required data set. ```php use Symfony\Component\DomCrawler\Crawler; $crawler = new Crawler($content); $crawler ->filter('ItemsList > Item') ->each(function (Crawler $element) { $data = $element ->children() ->extract(['_name', '_text']); var_dump($data); }); // Result: array:2 [ 0 => array:2 [ 0 => "id", 1 => "1" ], 1 => array:2 [ 0 => "title", 1 => "Foo Bar" ] ] ``` Commits ------- 79162c1 [DomCrawler] Added return of element name in `extract()` method
2 parents 905119b + 79162c1 commit 4df912b

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

src/Symfony/Component/DomCrawler/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* Added return of element name (`_name`) in `extract()` method.
8+
49
4.2.0
510
-----
611

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,8 @@ public function extract($attributes)
658658
foreach ($attributes as $attribute) {
659659
if ('_text' === $attribute) {
660660
$elements[] = $node->nodeValue;
661+
} elseif ('_name' === $attribute) {
662+
$elements[] = $node->nodeName;
661663
} else {
662664
$elements[] = $node->getAttribute($attribute);
663665
}

src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ public function testExtract()
416416
$this->assertEquals(array(array(), array(), array()), $crawler->extract(array()), '->extract() returns empty arrays if the attribute list is empty');
417417

418418
$this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->extract('_text'), '->extract() returns an empty array if the node list is empty');
419+
420+
$this->assertEquals(array(array('One', 'li'), array('Two', 'li'), array('Three', 'li')), $crawler->extract(array('_text', '_name')), '->extract() returns an array of extracted data from the node list');
419421
}
420422

421423
public function testFilterXpathComplexQueries()

0 commit comments

Comments
 (0)