How to get email address from a long string

前端 未结 13 1913
情书的邮戳
情书的邮戳 2020-11-27 04:29

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 a

相关标签:
13条回答
  • 2020-11-27 04:54

    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.

    0 讨论(0)
提交回复
热议问题