0

Best practice for parsing HTML in PHP? Use DOM

http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php

 

[code]

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
$xml->loadHTML($html);

// 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;
}

[/code]
facebooktwittergoogle plus


Leave a Reply