Output From PHP Function parseNumber( $numberArray, $callingcode )





IDD prefix [1]: + // Country Code[2]: 1 // NDD prefix [3]: // Number [4]: // [noError]: TRUE

International Format: 1 // Field [2] and [4] above! // Also see the free sample PHP code below!


This product includes GeoLite data created by MaxMind, available from http://maxmind.com/ licensed CC BY-SA 3.0 Highway to Market






Free Sample PHP Code




include 'geoip.inc';
include 'callingcode.inc';
include 'numberparser.inc';

$gi = geoip_open( 'GeoIP.dat', GEOIP_STANDARD );

if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
    $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    
$countrycode = geoip_country_code_by_addr( $gi, $ipaddress );

$acceptLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE'];

$pattern = '/-'                                   // Delimiter '/' and '-' preceding country code
         . '(AD|AE|(?#RegExLib.com)|ZM|ZW)'       // Regular Expression matching ISO 3166 Country Codes
         . '(?(?=.*q=)(?=.*(q=0\.[7-9]|q=1))|.*)' // Country code must be of a quality value q=0.7 or higher
         . '/';                                   // Delimiter '/'

if ( preg_match( $pattern, $acceptLanguage, $matches ) )
    $countrycodeAcceptLanguage = $matches[1];     // [1] for first parenthesized subpattern

// Decide on a logic to handle the case $countrycode <> $countrycodeAcceptLanguage
if ( !$countrycode )         // If no geograhic location is established; use Accept-Language header
	$countrycode = $countrycodeAcceptLanguage;

if ( $countrycode != $countrycodeAcceptLanguage )
	$countrycode = '';   // Reset $countrycode if geograhic location differs from Accept-Language header                    


$callingcode = getCountryCallingCode( $countrycode );

$numberArray[0] = '+1 (202) 456-1414'; // Example input data!

$parseOK = parseNumber( $numberArray, $callingcode ); 

if ( parseNumber( $numberArray, $callingcode ) )
{
	echo htmlentities( $numberArray[0] ); // Number as input, not changed by function
	echo htmlentities( $numberArray[1] ); // IDD prefix (+|00|(?#Country specific IDD))
	echo htmlentities( $numberArray[2] ); // Country Code
	echo htmlentities( $numberArray[3] ); // NDD prefix
	echo htmlentities( $numberArray[4] ); // subscriber number
}