MSISDN is international standard for phone numbers. Billing systems required to identified two major cases where some of the phone numbers are international, and some others are local numbers without the international extension. Most of the billing systems handle this scenarios in different approaches and styles.

Here is a simple code that make this trick in BillRun, including cases where the number including more characters (such as leading zeros, hyphens, brackets, etc):

 /**
* method to check if phone number is intl number or local number base on msisdn standard
*
* @param string $phoneNumber the phone number to check
*
* @return boolean true in case is international phone number else false
*/
public static function isIntlNumber($phoneNumber) {

$cleanNumber = self::cleanLeadingZeros(preg_replace("/[^0-9]/", "", $phoneNumber));

//CCNDCSN - First part USA; second non-USA
if (preg_match("/^(1[2-9]{1}[0-9]{2}|[2-9]{1}[0-9]{1,2}[1-9]{1}[0-9]{0,2})[0-9]{7}$/", $cleanNumber)) {
return true;
}

return false;
}

/**
* method to clean leading zero of phone number
*
* @param string $number
*
* @return string the number without leading zeros
*/
public static function cleanLeadingZeros($number) {
return ltrim($number, "0");
}

Thanks to the code above we can handle two different scenarios that mentioned above - same field have MSISDN and ISDN (later is without intl extension):

 /**
* method to convert phone number to msisdn
*
* @param string $phoneNumber the phone number to convert
* @param string $defaultPrefix the default prefix to add
* @param boolean $cleanLeadingZeros decide if to clean leading zeros on return value
*
* @return string phone number in msisdn format
*/
public static function msisdn($phoneNumber, $defaultPrefix = null, $cleanLeadingZeros = true) {

if (empty($phoneNumber)) {
return $phoneNumber;
}

settype($phoneNumber, 'string');

if ($cleanLeadingZeros) {
$phoneNumber = self::cleanLeadingZeros($phoneNumber);
}

if (self::isIntlNumber($phoneNumber)) {
return $phoneNumber;
}

if (is_null($defaultPrefix)) {
$defaultPrefix = Billrun_Factory::config()->getConfigValue('billrun.defaultCountryPrefix', 972);
}
return $defaultPrefix . $phoneNumber;
}

You can find the full source code of the MSISDN validation methods (and more useful methods) in our general util class.

If you find more improvements or have questions feel free to comment.