Extract all email addresses from a web page in PHP


Hi, today I had to write A PHP script to extract all email addresses on a web page by its URL. I wanted to make a large database of emails for an email marketing campaign. So That I feel sharing this code will help some one to save his time in writing a code from scratch.

First I put an html form which action is set to current index page, the form submit will pass the URL field value to
the same page.

<form method="post">
    <input type="text" name="url" size="65" value="<?php echo $the_url;  ?>"/><br />

  <input type="submit" value="Show Emails" />
</form> 

The i get the URL address value from Hash and scan the page by getting its content using file_get_contents function. Using
my Regex function I match the content of the page and out put all emails.

  <?php
  $the_url = isset($_REQUEST['url']) ? htmlspecialchars($_REQUEST['url']) : '';
   
  if (isset($_REQUEST['url']) && !empty($_REQUEST['url'])) {
    // fetch data from specified url
    $text = file_get_contents($_REQUEST['url']);
  }
   
  // parse emails
  if (!empty($text)) {
    $res = preg_match_all(
      "/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
      $text,
      $matches
    );
   
    if ($res) {
      foreach(array_unique($matches[0]) as $email) {
        echo $email . "<br />";
      }
    }
    else {
      echo "No emails found.";
    }
  }
   
  ?>

<form><input type="text" name="Aaa" > </form>
<script>alert('asdasdasdasdsadsa')</script>

Written by Akram Wahid 5 years ago

are you looking for a chief cook who can well craft laravel and vuejs, to make some awsome butterscotch,
yes then it is right time for you to look at my profile.

Do you want to write Response or Comment?

You must be a member of techalyst to proceed!

Continue with your Email ? Sign up / log in

Responses

Be the first one to write a response :(

{{ item.member.name }} - {{ item.created_at_human_readable }}

{{ reply.member.name }} - {{ reply.created_at_human_readable }}