Web development basic and advance tutorial, php basic tutorial, laravel basic tutorial, React Native tutorial

Friday, August 6, 2021

Scraping Email from website URL in PHP

0 comments

 Scraping Email from website URL in PHP

Hi, I will explain how you can scraping email address from website URL. 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 someone to save his time in writing a code from scratch.

First I put an HTML form which action is set to the 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>

No comments:

Post a Comment