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.


Copy this code and paste it in your HTML
  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

You need to login to post a comment.