Best practice for parsing HTML in PHP? Use DOM




function cfriend_get_href_links($html) {

// Create a new DOM Document to hold our webpage structure
$xml = new DOMDocument();

// Load the url’s contents into the DOM

// Empty array to hold all links to return
$links = array();

//Loop through each <a> tag in the dom and add it to the link array
foreach($xml->getElementsByTagName(‘a’) as $link) {
$links[] = array(‘url’ => $link->getAttribute(‘href’), ‘text’ => $link->nodeValue);

//Return the links
return $links;

facebooktwittergoogle plus

Leave a Reply