Posted By

gdvickery on 03/02/09


Tagged

regex file html links extract


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

Vordreller
rezzz
vali29
jodm


Extract all links from a page using Regex


 / Published in: PHP
 

These 4 lines of code can be used to extract and return all links from any page using regex patterns. If links are found, an array is returned, else boolean false is returned.

  1. $pageData = file_get_contents('http://geekforbrains.com');
  2. if(preg_match_all('/<a\s+href=["\']([^"\']+)["\']/i', $pageData, $links, PREG_PATTERN_ORDER))
  3. return array_unique($links[1]);
  4. return false;

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Unreal on March 11, 2009

This is a bit more efficient

http://snipplr.com/view/12678/find-all-the-links-on-a-page/

You need to login to post a comment.