In PHP, how do I extract multiple e-mail addresses from a block of text and put them into an array?

前端 未结 7 2146
时光说笑
时光说笑 2020-11-30 03:19

I have a block of text from which I want to extract the valid e-mail addresses and put them into an array. So far I have...

   $string = file_get_contents(\         


        
相关标签:
7条回答
  • 2020-11-30 03:42

    This detects all mail addresses:

    $sourceeee= 'Here are examplr mymail@yahoo.com and my-e.mail@goog.com or something more';
    
    preg_match_all('/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/i', $sourceeee, $found_mails);
    

    then you can use $found_mails[0] array.

    0 讨论(0)
  • 2020-11-30 03:44

    I know this is not the question you asked but I noticed that your regex is not accepting any address like 'myemail@office21.company.com' or any address with a subdomain. You could replace it with something like :

    /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/
    

    which will reject less valid e-mail (although it is not perfect).

    I also suggest you read this article on e-mail validation, it is pretty good and informative.

    0 讨论(0)
  • 2020-11-30 03:45

    It worked better for me:

    <?php
    $content = "Hi my name is Joe, I can be contacted at joe@mysite.com.";
    preg_match("/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/i", $content, $matches);
    print $matches[0];
    ?>
    

    Some of the others didn't accept domains like: name@example.com.sv

    I found it on: http://snipplr.com/view/63938/

    0 讨论(0)
  • 2020-11-30 03:47

    This regex will extract all unique email address from a url or file and output each in new line. It will consider all subdomains and prefix suffix issues. Find comfortable to use it.

    <?
    $url="http://example.com/";
    $text=file_get_contents($url);
    $res = preg_match_all(
    "/[a-z0-9]+[_a-z0-9\.-]*[a-z0-9]+@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})/i",
    $text,
    $matches
    );
    if ($res) {
    foreach(array_unique($matches[0]) as $email) {
    echo $email . "<br />";
    }
    }
    else {
    echo "No emails found.";
    }
    ?>
    

    check here for more reference : http://www.php.net/manual/en/function.preg-match-all.php

    0 讨论(0)
  • 2020-11-30 03:54

    This function works fine without using regex. So it is really faster and low resource hungry.

    <?php
    function extract_email_addresses($str){
        $emails = array();
        $str = strip_tags( $str );
        $str = preg_replace('/\s+/', ' ', $str); 
        $str = preg_replace("/[\n\r]/", "", $str); 
        $remove_chars = array (',', "<", ">", ";", "'", ". ");
        $str = str_replace( $remove_chars, ' ', $str );
        $parts = explode(' ', $str);
        if(count($parts) > 0){
            foreach($parts as $part){
                $part = trim($part);
                if( $part != '' ) {
                    if( filter_var($part, FILTER_VALIDATE_EMAIL) !== false){
                        $emails[] = $part;
                    }                
                }
            }
        }
        if(count($emails) > 0){
            return $emails;
        }
        else{
            return null;
        }
    }
    
    $string = "Guys, please help me to extract valid sam-ple.1990@gmail.co.uk email addresses from some text content using php
    example , i have below text content in mysql database ' Life is more beautiful, and i like to explore lot please email me to sample@gmail.com. Learn new things every day. 'from the above text content i want to extract email address 'sample-x@gmail.com' using php regular expressions or other method.";
    
    $matches = extract_email_addresses( $string );
    print_r($matches);
    
    ?>
    
    0 讨论(0)
  • 2020-11-30 03:57

    Your code is almost perfect, you just need to replace preg_match(...) with preg_match_all(...)

    http://www.php.net/manual/en/function.preg-match.php

    http://www.php.net/manual/en/function.preg-match-all.php

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