How to get email address from a long string

前端 未结 13 2000
情书的邮戳
情书的邮戳 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:36

    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;
    }
    

提交回复
热议问题