Symfony DomCrawler example

Compont 5.0 Document


	$html = <<<'HTML'
<!DOCTYPE html>
<html>
    <body>
    	<dl class='item'>
    		<dd>
    			<a href=1>a</a>
			</dd>
		</dl>
		<dl class='item'>
    		<dd>
    			<a href=2>b</a>
			</dd>
		</dl>
    </body>
</html>
HTML;
	// $html = file_get_contents('url');

    $crawler = new Crawler($html);

    $chs = $crawler->filter('dl.item > dd > a');

    $a = [];
    if ($chs->count() > 0) {
        foreach($chs as $ch) {
            /** @var \DOMElement $ch */
            $a[] = [
                'url' => $ch->getAttribute('href'),
                'name' => $ch->textContent
            ];
        }
    }