note 95894 added to class.domdocument
| From: | jay at jaygilford dot com | Date: | Wed, 27 Jan 2010 16:46:16 +0000 |
| Subject: | note 95894 added to class.domdocument | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165381@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
(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;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 80.41.192.175
----
Manual Page -- http://www.php.net/manual/en/class.domdocument.php
Edit -- https://master.php.net/note/edit/95894
Del: integrated -- https://master.php.net/note/delete/95894/integrated
Del: useless -- https://master.php.net/note/delete/95894/useless
Del: bad code -- https://master.php.net/note/delete/95894/bad+code
Del: spam -- https://master.php.net/note/delete/95894/spam
Del: non-english -- https://master.php.net/note/delete/95894/non-english
Del: in docs -- https://master.php.net/note/delete/95894/in+docs
Del: other reasons-- https://master.php.net/note/delete/95894
Reject -- https://master.php.net/note/reject/95894
Search -- https://master.php.net/manage/user-notes.php