Skip to content

Instantly share code, notes, and snippets.

@yyano
Last active October 8, 2017 14:20
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 yyano/51c638125f66753374c6fab3d0f22c17 to your computer and use it in GitHub Desktop.
Save yyano/51c638125f66753374c6fab3d0f22c17 to your computer and use it in GitHub Desktop.
E.164 Format phone Number, Convert to contry-code and Domestic phone number.
<?php
var_dump(convertE164toDomestic("+81399999999");
public static function convertFromE164($E164Number)
{
$contryLength = 1;
$country = substr($E164Number, 1, $contryLength);
switch ($country) {
case '1': $contryLength = 1; break;
case '7': $contryLength = 1; break;
case '2': $contryLength = 2;
$country = substr($E164Number, 1, $contryLength);
if($country === "20"
OR $country === "27") break;
else {
$contryLength = 3;
$country = substr($E164Number, 1, $contryLength);
}
break;
case '3': $contryLength = 2;
$country = substr($E164Number, 1, $contryLength);
if($country === "30"
OR $country === "31"
OR $country === "32"
OR $country === "33"
OR $country === "34"
OR $country === "36"
OR $country === "39") break;
else {
$contryLength = 3;
$country = substr($E164Number, 1, $contryLength);
}
break;
case '4': $contryLength = 2;
$country = substr($E164Number, 1, $contryLength);
if($country === "40"
OR $country === "41"
OR $country === "43"
OR $country === "44"
OR $country === "45"
OR $country === "46"
OR $country === "47"
OR $country === "48"
OR $country === "49") break;
else {
$contryLength = 3;
$country = substr($E164Number, 1, $contryLength);
}
break;
case '5': $contryLength = 2;
$country = substr($E164Number, 1, $contryLength);
if($country === "51"
OR $country === "52"
OR $country === "53"
OR $country === "54"
OR $country === "55"
OR $country === "56"
OR $country === "57"
OR $country === "58") break;
else {
$contryLength = 3;
$country = substr($E164Number, 1, $contryLength);
}
break;
case '6': $contryLength = 2;
$country = substr($E164Number, 1, $contryLength);
if($country === "60"
OR $country === "61"
OR $country === "62"
OR $country === "63"
OR $country === "64"
OR $country === "65"
OR $country === "66") break;
else {
$contryLength = 3;
$country = substr($E164Number, 1, $contryLength);
}
break;
case '8': $contryLength = 2;
$country = substr($E164Number, 1, $contryLength);
if($country === "81"
OR $country === "82"
OR $country === "84"
OR $country === "86") break;
else {
$contryLength = 3;
$country = substr($E164Number, 1, $contryLength);
}
break;
case '9': $contryLength = 2;
$country = substr($E164Number, 1, $contryLength);
if($country === "90"
OR $country === "91"
OR $country === "92"
OR $country === "93"
OR $country === "94"
OR $country === "95"
OR $country === "98") break;
else {
$contryLength = 3;
$country = substr($E164Number, 1, $contryLength);
}
break;
default:
$country = 0;
$contryLength = 0;
break;
}
unset($e164);
$e164["contory"] = substr($E164Number, 1, $contryLength);
$e164["telNumber"] = "0" . substr($E164Number, $contryLength + 1);
return $e164;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment