How to get All URLs From a Page / Paragraph ?

Friday, February 19, 2010 3:42
Posted in category PHP Interview Questions
<?php

$string = '<a href="http://www.test.com">test.com</a> has many links with
tests <a href="http://www.test.net/file.php">links</a> to many sites and
even urls without links like http://www.test.org just to fill the gaps and
not to forget this one http://www.imjan.com/php-interview-questions/curl-register-your-domain-but-check-its-availability/
which has a space after it. The script has been modifiied from its original so now
it grabs ssl such as https://www.test.com/abc.php also';

function getAllUrls($string){
    $regex = '/https?\:\/\/[^\" ]+/i';
    preg_match_all($regex, $string, $matches);
    return ($matches[0]);
}

 $urls = getAllUrls($string);

 foreach($urls as $url):
  echo $url.'<br />';
 endforeach;

?>
  • Share/Bookmark
You can leave a response, or trackback from your own site.

One Response to “How to get All URLs From a Page / Paragraph ?”

  1. Henriette Karpf says:

    February 20th, 2010 at 1:26 am

    hey this blog is great. I’m glad I came by this blog. Maybe I can contribute in the near future. PM ME on Yahoo AmandaLovesYou702 Thank you day418

Leave a Reply