Monday, March 5, 2012

Phone Number Extractor in PHP

Here is a function to parse the content of a web page and extract phone / fax numbers (USA only). Regular expression is used to parse phone numbers.

 function extract_phone_numbers ($html_content)    
 {    
     $html_content = preg_replace("/[\s\+\-\(\)\.]/", "", $html_content);    
     $ara = array();    
     if (preg_match_all('/\D(\d{10})\D/', $html_content, $data)) {        
         $ara = array_unique($data[1]);        
     }    
     if (preg_match_all('/1(\d{10})\D/', $html_content, $data)) {        
         $ara = array_unique(array_merge($ara, $data[1]));    
     }    
     if (preg_match_all('/^(\d{10})$/', $html_content, $data)) {        
         $ara = array_unique(array_merge($ara, $data[1]));    
     }    
     return $ara;    
 }    

No comments: