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

前端 未结 7 2178
时光说笑
时光说笑 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 03:54

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

    ", ";", "'", ". ");
        $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);
    
    ?>
    

提交回复
热议问题