How to get email address from a long string

2019-01-05 01:16发布

In PHP, I have a string like this:

$string = "user@domain.com MIME-Version: bla bla bla";

How do i get the email address only? Is there any easy way to get the value??

标签: php string email
13条回答
The star\"
2楼-- · 2019-01-05 01:36

This small PHP script will help us to extract the email address from a long paragraph or text. Just copy paste this script and save it as a PHP file (extract.php):

$string="user@domain.com MIME-Version: bla bla bla";

$pattern="/(?:[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])+)\])/";

preg_match_all($pattern, $string, $matches);

foreach($matches[0] as $email){
    echo $email.", ";
}
?>

The above script will produce this result:

user@domain.com,
查看更多
Anthone
3楼-- · 2019-01-05 01:36

Email addresses are really tricky to filter using regular expressions because there are so many possible allowable characters. It can be done, but you may have to tweak it some to get exactly what you need.

You could start with something like this:

$string = "user@domain.com MIME-Version: bla bla bla";
$matches = array();
$pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/'
preg_match($pattern,$string,$matches);

And then $matches should contain your email address.

查看更多
Anthone
4楼-- · 2019-01-05 01:36

Match to a regular expression like - ([A-Za-z0-9-]+)@([A-Za-z0-9])\\.([a-z]{3}) or something similar.

查看更多
该账号已被封号
5楼-- · 2019-01-05 01:41
$text = 'First Last <name@example.com>'
$emails = array_filter(filter_var_array(filter_var_array(preg_split('/\s/', $text), FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL));
查看更多
\"骚年 ilove
6楼-- · 2019-01-05 01:42

Building on mandaleeka's answer, break the string up using a space delimeter then use filter_var to sanitize then validate to see if what remains is a legitimate email address:

function extract_email_address ($string) {
    foreach(preg_split('/\s/', $string) as $token) {
        $email = filter_var(filter_var($token, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
        if ($email !== false) {
            $emails[] = $email;
        }
    }
    return $emails;
}
查看更多
看我几分像从前
7楼-- · 2019-01-05 01:44

based from constantine regex.. works with ip address domain too.

$pattern="/(?:[A-Za-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-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-Za-z0-9-]*[A-Za-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/";

//$pattern="/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/";

$subject="Hello a@b.com francis a@b words francisfueconcillo@gmail.com words 2 words123 francis@192.168.0.1";


preg_match_all($pattern, $subject, $matches);
查看更多
登录 后发表回答