Posted By

gdvickery on 03/02/09


regex file html links

Viewed 9 times

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('');
  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


RSS Icon Subscribe to comments

You need to login to post a comment.