Extracting emails from a webpage can be very useful for a variety of tasks, from data gathering for a research project to populating a contact list for a marketing campaign. In this blog post, we'll discuss a short PHP script that does exactly that, using simple functions and a regular expression.
The PHP code snippet provided starts by defining a URL for a webpage.
$url = "https://devpicker.com/"; // Change with your desired URL
Next, the PHP file_get_contents
function is used to read the entire file into a string.
$content = file_get_contents($url);
Now that we have the content of the webpage stored in the $content
variable, we will use the preg_match_all
function, which performs a global regular expression match.
$res = preg_match_all("/[a-z0-9]+[_a-z0-9.-]*[a-z0-9]+@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})/i", $content, $matches);
This function searches the entire string $content
for matches to the pattern specified by the regular expression. This regular expression matches any sequence of characters that forms a valid email address.
The preg_match_all
function then returns the number of full pattern matches found, which is stored in $res
, and an array of the matched strings, stored in $matches
.
In the following section, we then check if any matches were found (if ($res)
). If so, we process each unique email address with a foreach loop, outputting it with an echo statement:
if ($res) {
foreach(array_unique($matches[0]) as $email) {
echo $email . "
";
}
}
If no matches were found, a simple message is displayed instead:
else {
echo "No emails found.";
}
That's it! With this short PHP script, you can easily extract email addresses from any webpage's content. Be aware that when using scripts like this one, it's important to respect privacy laws and the terms of use of the website you're extracting data from.
This PHP script can serve a variety of purposes in different contexts. Here are some of the potential use cases:
Data Mining: This script can be used to mine data for research purposes, specifically for collecting public email addresses from web pages.
Lead Generation: Businesses can use this script to find potential leads for their services or products. However, it's important to respect privacy laws and to always ask for consent before using email addresses for marketing purposes.
Building a Contact List: If you're an event organizer or a community manager, this script can help you quickly build a contact list by extracting email addresses from relevant web pages.
Research: Journalists, researchers, or students can use this script to gather contact information for a study, investigation, or project.
Here is the full code snippet that we just discussed:
$url = "https://devpicker.com/"; // Change with your desired URL
$content = 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", $content, $matches);
if ($res) {
foreach(array_unique($matches[0]) as $email) {
echo $email . "
";
}
} else {
echo "No emails found.";
}