Archive for October, 2009

how to get all links from a web page

Monday, October 26th, 2009

A question that gets asked all the time on forums is “How do I get all links on a web page” inside of <a> tags, so here’s some code with full commenting for each line

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * @author Jay Gilford
 */
 
// regular expression pattern to match all links on a page
$pattern = '%<a [^>]+href="(?P<url>[^"]+)"[^>*]*>(?P<text>[^< ]+)</a>%si';
 
// Webpage URL to get links from
$url = 'http://www.jaygilford.com/';
 
// Fetch contents of whole page
$page_content = file_get_contents($url);
 
// Get all matches of links and put them into the $matches variable
preg_match_all($pattern, $page_content, $matches);
 
// Variable to hold all of our urls and their text
$urls = array();
 
// Loop through each array item
foreach($matches['url'] as $k=>$v) {
    // combine the url and text into it's own key for ease of access
    $urls[$k] = array('url' => $v,'text' => $matches['text'][$k]);
}
 
// For display purposes only to show the contents of $urls
echo print_r($urls, true);

If you have any questions regarding this feel free to contact me. Details can be found on the about page