Monday, March 5, 2012

Email extractor in PHP using regex

Today, I am sharing my PHP code that extracts email address from html source of an URL. It uses regular expression to parse email address.

 function extract_email_addresses ($html_source)    
 {    
     $html_source = str_replace("(at)", "@", $html_source);    
     $html_source = str_replace("[at]", "@", $html_source);    
     $html_source = str_replace("(dot)", ".", $html_source);    
     $html_source = str_replace("[dot]", ".", $html_source);    
     $html_source = strtolower($html_source);    
     $ara = array();    
     if (preg_match_all('/([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))/', $html_source, $data)) {        
         $ara = array_unique($data[1]);    
     }    
     return $ara;    
 }    
Please share your thoughts to improve the function.

No comments: