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;
}
Showing posts with label Regular Expression. Show all posts
Showing posts with label Regular Expression. Show all posts
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.
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.
Subscribe to:
Posts (Atom)