I want to extract email address from a string, for example:
<?php // code
$string = 'Ruchika <ruchika@example.com>';
?>
From the above string I only want to get email address ruchika@example.com
.
Kindly, recommend how to achieve this.
I want to extract email address from a string, for example:
<?php // code
$string = 'Ruchika <ruchika@example.com>';
?>
From the above string I only want to get email address ruchika@example.com
.
Kindly, recommend how to achieve this.
Try this
<?php
$string = 'Ruchika < ruchika@example.com >';
$pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
preg_match_all($pattern, $string, $matches);
var_dump($matches[0]);
?>
see demo here
Second method
<?php
$text = 'Ruchika < ruchika@example.com >';
preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $text, $matches);
print_r($matches[0]);
?>
See demo here
Parsing e-mail addresses is an insane work and would result in a very complicated regular expression. For example, consider this official regular expression to catch an e-mail address: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
Amazing right?
Instead, there is a standard php function to do this called mailparse_rfc822_parse_addresses()
and documented here.
It takes a string as argument and returns an array of associative array with keys display, address and is_group.
So,
$to = 'Wez Furlong <wez@example.com>, doe@example.com';
var_dump(mailparse_rfc822_parse_addresses($to));
would yield:
array(2) {
[0]=>
array(3) {
["display"]=>
string(11) "Wez Furlong"
["address"]=>
string(15) "wez@example.com"
["is_group"]=>
bool(false)
}
[1]=>
array(3) {
["display"]=>
string(15) "doe@example.com"
["address"]=>
string(15) "doe@example.com"
["is_group"]=>
bool(false)
}
}
try this code.
<?php
function extract_emails_from($string){
preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
return $matches[0];
}
$text = "blah blah blah blah blah blah email2@address.com";
$emails = extract_emails_from($text);
print(implode("\n", $emails));
?>
This will work.
Thanks.
This is based on Niranjan's response, assuming you have the input email enclosed within < and > characters). Instead of using a regular expression to grab the email address, here I get the text part between the < and > characters. Otherwise, I use the string to get the entire email. Of course, I didn't make any validation on the email address, this will depend on your scenario.
<?php
$string = 'Ruchika <ruchika@example.com>';
$pattern = '/<(.*?)>/i';
preg_match_all($pattern, $string, $matches);
var_dump($matches);
$email = $matches[1][0] ?? $string;
echo $email;
?>
Here is a forked demo.
Of course, if my assumption isn't correct, then this approach will fail. But based on your input, I believe you wanted to extract emails enclosed within < and > chars.
This function extract all email from a string and return it in an array.
function extract_emails_from($string){
preg_match_all( '/([\w+\.]*\w+@[\w+\.]*\w+[\w+\-\w+]*\.\w+)/is', $string, $matches );
return $matches[0];
};
This works great and it's minimal:
$email = strpos($from, '<') ? substr($from, strpos($from, '<') + 1, -1) : $from
use (my) function getEmailArrayFromString
to easily extract email adresses from a given string.
<?php
function getEmailArrayFromString($sString = '')
{
$sPattern = '/[\._\p{L}\p{M}\p{N}-]+@[\._\p{L}\p{M}\p{N}-]+/u';
preg_match_all($sPattern, $sString, $aMatch);
$aMatch = array_keys(array_flip(current($aMatch)));
return $aMatch;
}
// Example
$sString = 'foo@example.com XXX bar@example.com XXX <baz@example.com>';
$aEmail = getEmailArrayFromString($sString);
/**
* array(3) {
[0]=>
string(15) "foo@example.com"
[1]=>
string(15) "bar@example.com"
[2]=>
string(15) "baz@example.com"
}
*/
var_dump($aEmail);
Based on Priya Rajaram's code, I have optimised the function a little more so that each email address only appears once.
If, for example, an HTML document is parsed, you usually get everything twice, because the mail address is also used in the mailto link, too.
function extract_emails_from($string){
preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
return array_values(array_unique($matches[0]));
}
This will work even on subdomains. It extracts all emails from text.
$marches[0]
has all emails.
$pattern = "/[a-zA-Z0-9-_]{1,}@[a-zA-Z0-9-_]{1,}(.[a-zA-Z]{1,}){1,}/";
preg_match_all ($pattern , $string, $matches);
print_r($matches);
$marches[0]
has all emails.
Array
(
[0] => Array
(
[0] => clotdesormakilgehr@prednisonecy.com
[1] => **********@******.co.za.com
[2] => clotdesormakilgehr@prednisonecy.com
[3] => clotdesormakilgehr@prednisonecy.mikedomain.com
[4] => clotdesormakilgehr@prednisonecy.com
)
[1] => Array
(
[0] => .com
[1] => .com
[2] => .com
[3] => .com
[4] => .com
)
)
A relatively straight forward approach is to use PHP built-in methods for splitting texts into words and validating E-Mails:
function fetchEmails($text) {
$words = str_word_count($text, 1, '.@-_1234567890');
return array_filter($words, function($word) {return filter_var($word, FILTER_VALIDATE_EMAIL);});
}
Will return the e-mail addresses within the text variable.