Skip to content

Instantly share code, notes, and snippets.

@alkemann
Created October 31, 2011 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alkemann/1328992 to your computer and use it in GitHub Desktop.
Save alkemann/1328992 to your computer and use it in GitHub Desktop.
get valid phone numbers
function _oneValidPhoneNumber($in) {
$stripped = preg_replace("/^[^0-9+]*[+]*46/", "+46", $in);
$stripped = preg_replace("/^[^0-9+]*[+]*47/", "+47", $in);
$stripped = preg_replace("/[^0-9+]/", "", $stripped);
$stripped = preg_replace("/^0046/", "+46", $stripped);
$stripped = preg_replace("/^0047/", "+47", $stripped);
if (!$stripped) return false;
if ($stripped[0] == '0') {
$stripped = substr($stripped, 1);
}
$strlen = strlen($stripped);
if ($strlen < 7) return false;
if ($stripped[0] == '+') return $stripped;
switch ($strlen) {
case 7: case 8: case 9: case 10:
return "+46$stripped";
}
}
function getValidPhonenumber($in) {
if ($one = _oneValidPhoneNumber($in)) return $one;
$stripped = preg_replace("/[^0-9]{2,99}/", "#SPLIT#", $in);
$list = explode("#SPLIT#", $stripped);
$ret = array();
foreach ($list as $one) {
if ($found = _oneValidPhoneNumber($one)) $ret[] = $found;
}
if ($ret) return $ret;
$pattern = "/[ \-0-9]{7,12}/";
$found = preg_match_all($pattern, $in, $matches);
if ($matches && $matches[0]) {
$ret = array();
foreach ($matches[0] as $one)
if ($found = _oneValidPhoneNumber($one)) $ret[] = $found;
if ($ret)
return $ret;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment