How to extract emails from a webpage in PHP?

Sometimes we need to extract the emails contained in a piece of text or html document. Here you can find a simple script that extracts unique emails from a webpage.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?
   
    function get_emails_from_webpage($url)
    {
      $text=file_get_contents($url);
      $res = preg_match_all("/[a-z0-9]+[_a-z0-9\.-]*[a-z0-9]+@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})/i",$text,$matches);
      if ($res) {
          return array_unique($matches[0]);
      }
      else{
          return null;
      }
    }
    $url="http://foo.com/";
    $emails = function get_emails_from_webpage($url)
    if($emails != null){
      foreach($emails as $email) {
        echo $email . "<br />";
      }
    }
    else {
        echo "No emails found.";
    }  
?>

1 response on How to extract emails from a webpage in PHP?

  1. Buenas Daniel,

    Acabo de ver tu spinner español. Yo tambien he sacado uno gratis bastante interesante para los usuarios del foro de black hat seo que he iniciado hace poco.

    Tambien estoy montando de desarrollo de bots avanzados con imacros. Si te animas a participar.

Leave a Reply to Nicolas Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>