1

I have a telephone number and its type is varchar(32). It could be 03004748495,3004748495, 0300-4748495, 300-4748495,+92-300-4748495 or 923004748495. I want it to be converted in this format 923004748495 as an integer if it is not in this format. How will I achieve that? Any ideas?

Ali Zia
  • 3,825
  • 5
  • 29
  • 77
  • Show us what you have tried – Epodax Nov 11 '15 at 07:49
  • I had tried different things but they were not generic. i want a generic function to handle all these problems. I mean if I had `-` in the telephone, I used explode.. But that was just for one case. Not generic. – Ali Zia Nov 11 '15 at 07:51
  • Is the country code 92? And is it implied in cases where it is not present like "03..." means "923..."; "3...." means "923..." etc. ? – Kyle Domingo Nov 11 '15 at 07:54
  • You shouldn't use an integer for phone numbers. http://stackoverflow.com/questions/3483156/whats-the-right-way-to-represent-phone-numbers – Ivar Nov 11 '15 at 07:55
  • Yes 92 is the country code. And 0300 is the carrier code. – Ali Zia Nov 11 '15 at 07:55
  • @Ivar Actually my telephone type is varchar, but in order to send sms, the format has to be 92xxxxxxxxxx. Without any + or - in between. So im converting telephone number into that format. – Ali Zia Nov 11 '15 at 08:02
  • Alright. That makes sense. :) – Ivar Nov 11 '15 at 08:12

2 Answers2

3

I suggest using liphonenumber-for-php

Example code from the github page:

$swissNumberStr = "044 668 18 00";

// First call the class
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
try {

    // turn it into a object using a given geo code
    $swissNumberProto = $phoneUtil->parse($swissNumberStr, "CH"); 

    // validate the phonenumber
    $isValid = $phoneUtil->isValidNumber($swissNumberProto);

    if($isValid){

        // Produces "+41446681800"
        echo $result_number = $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::E164);
         // Produces "044 668 18 00"
        echo $result_number = $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::NATIONAL);
        // Produces "+41 44 668 18 00"
        echo $result_number = $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::INTERNATIONAL);
    }

} catch (\libphonenumber\NumberParseException $e) {
    var_dump($e);
}
Crecket
  • 718
  • 2
  • 7
  • 24
1

Use preg_replace to remove unwanted signs (http://php.net/manual/en/function.preg-replace.php).

<?php

$number = '+92-300-4748495';

$number = preg_replace('/[^0-9]/', '', $number);

echo $number;

If you want to remove the leading zero too, you could use rtrim (http://php.net/manual/en/function.ltrim.php)

<?php

$number = '0300-4748495';

$number = preg_replace('/[^0-9]/', '', $number);

$number = ltrim($number, '0');

echo $number;

Additional request "If 92 is not added at the start, Add 92 also."

<?php
$number = '123456';

if (substr($number, 0, 2) != '92') {
    $number = '92' . $number;
}

echo $number;
tino.codes
  • 1,512
  • 1
  • 12
  • 23