How to get email address from a long string

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

    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,
    

提交回复
热议问题