Skip to content

Instantly share code, notes, and snippets.

@chillbits-legacy
Last active July 15, 2016 08:16
Show Gist options
  • Save chillbits-legacy/005f104588e3314fc6e4b785b80d59f2 to your computer and use it in GitHub Desktop.
Save chillbits-legacy/005f104588e3314fc6e4b785b80d59f2 to your computer and use it in GitHub Desktop.
Get country code from phone number
<?php
function getCountryCode($phoneNumber) {
// get your list of country codes
$countries = getCountries();
$countries = array(array("US","1","US (+1)"),array("GB","44","GB (+44)")) + $countries;
foreach( $countries as $country )
{
if ( substr( $phoneNumber, 0, strlen( '+'.$country[1] ) ) == '+'.$country[1] )
{
// match
return $country[1];
}
}
return false;
}
function getCountries(){
return array(array("DZ","213","DZ (+213)"),array("AD","376","AD (+376)"),array("AO","244","A0 (+244)"),array("AI","1264","AI (+1264)"),array("AG","1268","AG (+1268)"),array("AR","54","AR (+54)"),array("AM","374","AM (+374)"),array("AW","297","AW (+297)"),array("AU","61","AU (+61)"),array("AT","43","AT (+43)"),array("AZ","994","AZ (+994)"),array("BS","1242","BS (+1242)"),array("BH","973","BH (+973)"),array("BD","880","BD (+880)"),array("BB","1246","BB (+1246)"),array("BY","375","BY (+375)"),array("BE","32","BE (+32)"),array("BZ","501","BZ (+501)"),array("BJ","229","BJ (+229)"),array("BM","1441","BM (+1441)"),array("BT","975","BT (+975)"),array("BO","591","B0 (+591)"),array("BA","387","BA (+387)"),array("BW","267","BW (+267)"),array("BR","55","BR (+55)"),array("BN","673","BN (+673)"),array("BG","359","BG (+359)"),array("BF","226","BF (+226)"),array("BI","257","BI (+257)"),array("KH","855","KH (+855)"),array("CM","237","CM (+237)"),array("CA","1","CA (+1)"),array("CV","238","CV (+238)"),array("KY","1345","KY (+1345)"),array("CF","236","CF (+236)"),array("CL","56","CL (+56)"),array("CN","86","CN (+86)"),array("CO","57","C0 (+57)"),array("KM","269","KM (+269)"),array("CG","242","CG (+242)"),array("CK","682","CK (+682)"),array("CR","506","CR (+506)"),array("HR","385","HR (+385)"),array("CU","53","CU (+53)"),array("CY","90392","CY (+90392)"),array("CY","357","CY (+357)"),array("CZ","42","CZ (+42)"),array("DK","45","DK (+45)"),array("DJ","253","DJ (+253)"),array("DM","1809","DM (+1809)"),array("DO","1809","DO (+1809)"),array("EC","593","EC (+593)"),array("EG","20","EG (+20)"),array("SV","503","SV (+503)"),array("GQ","240","GQ (+240)"),array("ER","291","ER (+291)"),array("EE","372","EE (+372)"),array("ET","251","ET (+251)"),array("FK","500","FK (+500)"),array("FO","298","FO (+298)"),array("FJ","679","FJ (+679)"),array("FI","358","FI (+358)"),array("FR","33","FR (+33)"),array("GF","594","GF (+594)"),array("PF","689","PF (+689)"),array("GA","241","GA (+241)"),array("GM","220","GM (+220)"),array("GE","7880","GE (+7880)"),array("DE","49","DE (+49)"),array("GH","233","GH (+233)"),array("GI","350","GI (+350)"),array("GR","30","GR (+30)"),array("GL","299","GL (+299)"),array("GD","1473","GD (+1473)"),array("GP","590","GP (+590)"),array("GU","671","GU (+671)"),array("GT","502","GT (+502)"),array("GN","224","GN (+224)"),array("GW","245","GW (+245)"),array("GY","592","GY (+592)"),array("HT","509","HT (+509)"),array("HN","504","HN (+504)"),array("HK","852","HK (+852)"),array("HU","36","HU (+36)"),array("IS","354","IS (+354)"),array("IN","91","IN (+91)"),array("ID","62","ID (+62)"),array("IR","98","IR (+98)"),array("IQ","964","IQ (+964)"),array("IE","353","IE (+353)"),array("IL","972","IL (+972)"),array("IT","39","IT (+39)"),array("JM","1876","JM (+1876)"),array("JP","81","JP (+81)"),array("JO","962","JO (+962)"),array("KZ","7","KZ (+7)"),array("KE","254","KE (+254)"),array("KI","686","KI (+686)"),array("KP","850","KP (+850)"),array("KR","82","KS (+82)"),array("KW","965","KW (+965)"),array("KG","996","KG (+996)"),array("LA","856","LA (+856)"),array("LV","371","LV (+371)"),array("LB","961","LB (+961)"),array("LS","266","LS (+266)"),array("LR","231","LR (+231)"),array("LY","218","LY (+218)"),array("LI","417","LI (+417)"),array("LT","370","LT (+370)"),array("LU","352","LU (+352)"),array("MO","853","MO (+853)"),array("MK","389","MK (+389)"),array("MG","261","MG (+261)"),array("MW","265","MW (+265)"),array("MY","60","MY (+60)"),array("MV","960","MV (+960)"),array("ML","223","ML (+223)"),array("MT","356","MT (+356)"),array("MH","692","MH (+692)"),array("MQ","596","MQ (+596)"),array("MR","222","MR (+222)"),array("YT","269","YT (+269)"),array("MX","52","MX (+52)"),array("FM","691","FM (+691)"),array("MD","373","MD (+373)"),array("MC","377","MC (+377)"),array("MN","976","MN (+976)"),array("MS","1664","MS (+1664)"),array("MA","212","MA (+212)"),array("MZ","258","MZ (+258)"),array("MN","95","MN (+95)"),array("NA","264","NA (+264)"),array("NR","674","NR (+674)"),array("NP","977","NP (+977)"),array("NL","31","NL (+31)"),array("NC","687","NC (+687)"),array("NZ","64","NZ (+64)"),array("NI","505","NI (+505)"),array("NE","227","NE (+227)"),array("NG","234","NG (+234)"),array("NU","683","NU (+683)"),array("NF","672","NF (+672)"),array("NP","670","NP (+670)"),array("NO","47","NO (+47)"),array("OM","968","OM (+968)"),array("PW","680","PW (+680)"),array("PA","507","PA (+507)"),array("PG","675","PG (+675)"),array("PY","595","PY (+595)"),array("PE","51","PE (+51)"),array("PH","63","PH (+63)"),array("PL","48","PL (+48)"),array("PT","351","PT (+351)"),array("PR","1787","PR (+1787)"),array("QA","974","QA(+974)"),array("RE","262","RE(+262)"),array("RO","40","RO(+40)"),array("RU","7","RU(+7)"),array("RW","250","RW(+250)"),array("SM","378","SM(+378)"),array("ST","239","ST(+239)"),array("SA","966","SA(+966)"),array("SN","221","SN(+221)"),array("CS","381","CS(+381)"),array("SC","248","SC(+248)"),array("SL","232","SL(+232)"),array("SG","65","SG(+65)"),array("SK","421","SK(+421)"),array("SI","386","SI(+386)"),array("SB","677","SB(+677)"),array("SO","252","SO(+252)"),array("ZA","27","ZA(+27)"),array("ES","34","ES(+34)"),array("LK","94","LK(+94)"),array("SH","290","SH(+290)"),array("KN","1869","KN(+1869)"),array("SC","1758","SC(+1758)"),array("SD","249","SD(+249)"),array("SR","597","SR(+597)"),array("SZ","268","SZ(+268)"),array("SE","46","SE(+46)"),array("CH","41","CH(+41)"),array("SI","963","SI(+963)"),array("TW","886","TW(+886)"),array("TJ","7","TJ(+7)"),array("TH","66","TH(+66)"),array("TG","228","TG(+228)"),array("TO","676","TO(+676)"),array("TT","1868","TT(+1868)"),array("TN","216","TN(+216)"),array("TR","90","TR(+90)"),array("TM","7","TM(+7)"),array("TM","993","TM(+993)"),array("TC","1649","TC(+1649)"),array("TV","688","TV(+688)"),array("UG","256","UG(+256)"),array("UA","380","UA(+380)"),array("AE","971","AE(+971)"),array("UY","598","UY(+598)"),array("UZ","7","UZ(+7)"),array("VU","678","VU(+678)"),array("VA","379","VA(+379)"),array("VE","58","VE(+58)"),array("VN","84","VN(+84)"),array("VG","1284","VG(+1284)"),array("VI","1340","VI(+1340)"),array("WF","681","WF(+681)"),array("YE","969","YE(+969)"),array("YE","967","YE(+967)"),array("ZM","260","ZM(+260)"),array("ZW","263","ZW(+263)"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment