Implement application to detect country by phone number.
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
class PhoneVerification
{
public $countryCodes = [];
public $status = "";
public $message = "";
public function loadCountriesCodes()
{
$data = '';
try
{
$data = file_get_contents(BASE_PATH."\data\countries.json");
}
catch (Exception $ex)
{
}
$json = json_decode($data, true);
foreach ($json as $data)
{
$this->countryCodes[$data["dialCode"]] = $data["country"];
}
return $this->countryCodes;
}
public function extractCountryCode($number)
{
$number = str_replace("+", "", $number);
$number = str_replace(" ", "", $number);
$codes = $this->loadCountriesCodes();
$digits = str_split($number);
$code = '';
foreach ($digits as $digit)
{
$code = $code.$digit;
if (array_key_exists($code, $codes))
{
break;
}
}
return $code;
}
public function setResult($status, $message)
{
$this->status = $status;
$this->message = $message;
}
}