Symfony\Component\DomCrawler\Crawler::each PHP Method

each() public method

The anonymous function receives the position and the node wrapped in a Crawler instance as arguments. Example: $crawler->filter('h1')->each(function ($node, $i) { return $node->text(); });
public each ( Closure $closure ) : array
$closure Closure An anonymous function
return array An array of values returned by the anonymous function
    public function each(\Closure $closure)
    {
        $data = array();
        foreach ($this->nodes as $i => $node) {
            $data[] = $closure($this->createSubCrawler($node), $i);
        }
        return $data;
    }

Usage Example

コード例 #1
0
 public function format(Crawler $crawler, array $config)
 {
     $locations = $config['locations'];
     $return = $crawler->each(function (Crawler $node, $i) use($locations) {
         // Symfony dom-crawler methods ignore text nodes, so we have to
         // revert to native DOMNode methods instead.
         $children = $node->getNode(0)->childNodes;
         $values = [];
         foreach ($children as $child) {
             // Skip non element or text nodes.
             if (!in_array($child->nodeType, [1, 3])) {
                 continue;
             }
             // Loop through locations but stop after the first truthy value.
             foreach ($locations as $location) {
                 if ('_text' === $location && ($value = trim($child->nodeValue))) {
                     $values[] = $value;
                     continue 2;
                 }
                 // DOMText does not have method hasAttribute.
                 if (1 === $child->nodeType && $child->hasAttribute($location) && ($value = trim($child->getAttribute($location)))) {
                     $values[] = $value;
                     continue 2;
                 }
             }
         }
         // Remove any lingering 'empty' elements.
         $value = implode(' ', array_filter(array_map('trim', $values)));
         if ($this->looksLikeGroupTitle($node)) {
             $value = '%%TITLE%%' . $value . '%%TITLE%%';
         }
         return $value;
     });
     return [array_shift($return)];
 }
All Usage Examples Of Symfony\Component\DomCrawler\Crawler::each