* @copyright 1997-2006 The PHP Group * @license http://www.gnu.org/copyleft/gpl.txt GNU General Public Licence v2 * @version CVS: $Id: IPv4.php,v 1.11 2005/11/29 12:56:35 fa Exp $ * @link http://pear.php.net/package/Numbers_CountryDialingCodes */ /** * Require PEAR.php. */ require_once 'PEAR.php'; // {{{ GLOBALS /** * Map of Country Dialing Codes to alpha2 Country Abbreviations. * * This array contains every valid dialing code as of writing. * * @global array $GLOBALS['Numbers_CountryDialingCodes_Country_Map'] */ $GLOBALS['Numbers_CountryDialingCodes_Country_Map'] = "1:US,CA,AI,AG,BB,BS,VG,VI,KY,BM,GD,TC,MS,MP,GU,LC,DM,VC,PR,DO,TT,KN,JM 20:EG 212:MA 213:DZ 216:TN 218:LY 220:GM 221:SN 222:MR 223:ML 224:GN 225:CI 226:BF 227:NE 228:TG 229:BJ 230:MU 231:LR 232:SL 233:GH 234:NG 235:TD 236:CF 237:CM 238:CV 239:ST 240:GQ 241:GA 242:CG 243:CD 244:AO 245:GW 246:IO 247:AC 248:SC 249:SD 250:RW 251:ET 252:SO 253:DJ 254:KE 255:TZ 256:UG 257:BI 258:MZ 260:ZM 261:MG 262:RE 263:ZW 264:NA 265:MW 266:LS 267:BW 268:SZ 269:KM,YT 27:ZA 290:SH 291:ER 297:AW 298:FO 299:GL 30:GR 31:NL 32:BE 33:FR 34:ES 350:GI 351:PT 352:LU 353:IE 354:IS 355:AL 356:MT 357:CY 358:FI 359:BG 36:HU 370:LT 371:LV 372:EE 373:MD 374:AM 375:BY 376:AD 377:MC 378:SM 379:VA 380:UA 381:CS,YU 385:HR 386:SI 387:BA 388:EU 389:MK 39:IT 40:RO 41:CH 420:CZ 421:SK 423:LI 43:AT 44:GB 45:DK 46:SE 47:NO 48:PL 49:DE 500:FK 501:BZ 502:GT 503:SV 504:HN 505:NI 506:CR 507:PA 508:PM 509:HT 51:PE 52:MX 53:CU 54:AR 55:BR 56:CL 57:CO 58:VE 590:GP 591:BO 592:GY 593:EC 594:GF 595:PY 596:MQ 597:SR 598:UY 599:AN 60:MY 61:AU,CC,CX 62:ID 63:PH 64:NZ 65:SG 66:TH 670:TL 672:AQ,NF 673:BN 674:NR 675:PG 676:TO 677:SB 678:VU 679:FJ 680:PW 681:WF 682:CK 683:NU 684:AS 685:WS 686:KI 687:NC 688:TV 689:PF 690:TK 691:FM 692:MH 7:RU,KZ 800:XF 808:XC 81:JP 82:KR 84:VN 850:KP 852:HK 853:MO 855:KH 856:LA 86:CN 870:XS 871:XE 872:XP 873:XI 874:XW 878:XU 880:BD 881:XG 882:XN 886:TW 90:TR 91:IN 92:PK 93:AF 94:LK 95:MM 960:MV 961:LB 962:JO 963:SY 964:IQ 965:KW 966:SA 967:YE 968:OM 970:PS 971:AE 972:IL 973:BH 974:QA 975:BT 976:MN 977:NP 979:XR 98:IR 991:XT 992:TJ 993:TM 994:AZ 995:GE 996:KG 998:UZ"; // }}} // {{{ Numbers_CountryDialingCodes /** * Class to access to decoding International phone numbers and dialing codes * * Provides methods for determining Country based on a phone number, country code * based on a Country abbreviation, and something else. * * @category Numbers * @package Numbers_CountryDialingCodes * @author Peter Beckman * @copyright 1997-2006 The PHP Group * @license http://www.php.net/license/3_01.txt PHP License 3.01 * @version CVS: $Id: IPv4.php,v 1.11 2005/11/29 12:56:35 fa Exp $ * @link http://pear.php.net/package/Numbers_CountryDialingCodes * @access public */ class Numbers_CountryDialingCodes { // {{{ properties var $dialing_iso3166 = ''; var $iso3166_dialing = ''; // }}} // {{{ _init_lookup_tables() /** * Read the Country Codes into a PHP hash if necessary. * * @param none * @return true * @access private */ function _init_lookup_tables() { if (is_array($this->dialing_iso3166)) { return true; } $data = explode("\n", $GLOBALS['Numbers_CountryDialingCodes_Country_Map']); foreach($data as $pair) { if (preg_match("/^(\d+)\:([A-Z]{2}(,[A-Z]{2})*)$/", $pair, $matches)) { $dial = $matches[1]; $codes = explode(',', $matches[2]); $hash_dialing_iso3166[$dial] = $codes; foreach($codes as $code) { $hash_iso3166_dialing[$code] = $dial; } unset($codes); } } $this->dialing_iso3166 =& $hash_dialing_iso3166; $this->iso3166_dialing =& $hash_iso3166_dialing; } // }}} // {{{ country_codes() /** * Returns the ISO-3166 alpha2 country code(s) for the given dialing code. * * Usage: * $cdc = new Numbers_CountryDialingCodes; * $array_countries = $cdc->country_codes(7); * echo $country_codes[0]; // 'RU'; * echo count($country_codes); // 2 ('RU' and 'KZ') * * @param int $dial International dialing access code * @return array Array of Country Codes; Empty array if not found */ function country_codes($dial) { $this->_init_lookup_tables(); if (is_array($this->dialing_iso3166)) { $result = $this->dialing_iso3166{$dial}; } if (isset($result)) { return $result; } return array(); } // }}} // {{{ dialing_code() /** * Returns the international dialing code for the given ISO-3166 alpha2 * country code, or false if no match could be found. * * Usage: * $cdc = new Numbers_CountryDialingCodes; * $dialing_code = $cdc->dialing_code('MX'); * echo $dialing_code; // 52, Mexico * * @param string $code ISO-3166 alpha2 country code * @return int Dialing code if found, otherwise false */ function dialing_code($code) { $this->_init_lookup_tables(); if (is_array($this->iso3166_dialing)) { return $this->iso3166_dialing[$code]; } return false; } // }}} // {{{ extract_dialing_code() /** * Extracts the international dialing code from the given international * telephone number. * * Usage: * $cdc = new Numbers_CountryDialingCodes; * $dialing_code = $cdc->extract_dialing_code('+524345852536'); * echo $dialing_code; // 52, Mexico * * @param string $phn International telephone number, for example * "+17035551212" or "+521234567890" * @return int Dialing code if found, otherwise false */ function extract_dialing_code($phn) { if (!preg_match("/^\+\d{4,}$/", $phn)) { return PEAR::raiseError("invalid international phone number: $phn"); } $this->_init_lookup_tables(); if (substr($phn,0,1) == '+') { $phn = substr($phn,1); } if (is_array($this->dialing_iso3166)) { $hash = $this->dialing_iso3166; } foreach($hash as $dialcode => $array_countries) { if (substr($phn,0,strlen($dialcode)) == $dialcode) { return $dialcode; } } return false; } // }}} } // }}} /* * vim: sts=4 ts=4 sw=4 cindent fdm=marker */ ?>