note 95894 modified in class.domdocument by thiago
| From: | thiago@php.net | Date: | Wed, 27 Jan 2010 16:58:35 +0000 |
| Subject: | note 95894 modified in class.domdocument by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165382@lists.php.net to get a copy of this message | ||
Here's a small function I wrote to get all page links using the DOMDocument which will
hopefully be of use to others
<?php
/**
* @author Jay Gilford
*/
/**
* get_links()
*
* @param string $url
* @return array
*/
function get_links($url) {
// Create a new DOM Document to hold our webpage structure
$xml = new DOMDocument();
// Load the url's contents into the DOM
$xml->loadHTMLFile($url);
// 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;
}
?>
--was--
Here's a small function I wrote to get all page links using the DOMDocument which will
hopefully be of use to others
(Original code at http://www.jaygilford.com/php/php-dom-get-all-pagelinks/)
<?php
/**
* @author Jay Gilford
*/
/**
* get_links()
*
* @param string $url
* @return array
*/
function get_links($url) {
// Create a new DOM Document to hold our webpage structure
$xml = new DOMDocument();
// Load the url's contents into the DOM
$xml->loadHTMLFile($url);
// 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;
}
?>
http://php.net/manual/en/class.domdocument.php