How to extract email addresses from a string in PHP?
In this article, we will discuss how to extract an email address from a given string in PHP. First of all, we have to understand what is a valid email address.
See also,
Basic rules for a valid email address
The following rules are for a valid email,
- Email addresses use only alphanumeric characters like- A-Z, a-z, 0-9, etc.
- Special characters like- ^ $ ! < > [ ] ,etc. are not allowed.
- Must have @ symbol.
- Except for the first character, hyphens ( – ), underscores ( _ ), periods ( . ) can be used within the address.
How to fetch email addresses from a given string in PHP
In PHP, email addresses can only be verified using methods like-preg_match(), preg_match_all() etc.
Let us write the script for retrieving email addresses,
<?php //String to be checked for email addresses $tstr = ' My first e-mail address is [email protected] My second e-mail address is [email protected] Obviously, this is not an email address: code__speedy@^gmail.co.in '; //test string for checking email $test_patt ="/(?:[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/"; //comapare using preg_match_all() method preg_match_all($test_patt, $tstr, $valid); //display list of emails foreach($valid[0] as $mail){ echo $mail."<br> "; } ?>
Output:-
[email protected] [email protected]
Let us shorten the pattern string and rewrite the script using the function,
<?php
function fetch_mails($text){
//String that recognizes an e-mail
$str = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
preg_match_all($str, $text, $out);
//return a blank array if not true otherwise insert the email in $out and return
return isset($out[0]) ? $out[0] : array();
}
//string to be checked
$tstr = '
My first e-mail address is [email protected]
My second e-mail address is [email protected]
Obviously this is not a emaail address: code__speedy@^gmail.co.in
';
//display email addresses
echo"<pre>";
print_r(fetch_mails($tstr));
?>
Output:-
Array
(
[0] => [email protected]
[1] => [email protected]
)
Let us rewrite the script without using the function that returns only unique email addresses,
Fetch email address from a string in PHP with this below program.
<?php
//String to be checked for emails
$tstr = '
My first e-mail address is [email protected]
My second e-mail address is [email protected]
Obviously, this is not an email address: code__speedy@^gmail.co.in
My last e-mail address is again [email protected]
';
//generate an empty array.
$match = array();
//String that recognize a e-mail
$test_pat = '/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/';
//check testpattern in given string
preg_match_all($test_pat, $tstr, $match);
echo"<pre>";
//seleccting only the unique ones
var_dump(array_values(array_unique($match[0])));
echo"</pre>";
//store above in array for upcoming bit.
$newArr = array_values(array_unique($match[0]));
//count no. of emails
$count_Arr = count($newArr);
//converting to one string
$mail_str = implode("<br>", $newArr);
//display the values
echo "<h3>Fetched uniqe $count_Arr email address are:</h3> $mail_str";
?>
Output:-
array(2) {
[0]=>
string(20) "[email protected]"
[1]=>
string(25) "[email protected]"
}
Fetched unique 2 email address are:
[email protected]
[email protected]
This is how we can retrieve email addresses from a given string in PHP. If you have any doubt, put a comment.
Leave a Reply