Jump to content

[SOLVED] Remove first character and replace with 44


Bricktop

Recommended Posts

Hi guys,

 

I'm working on a script which sends a phone number and message via XML to a server, this server then grabs the info and sends it off as a text/SMS message.

 

This is working great but the server only accepts numbers in MSISDN format (441234567890 instead of 01234567890)

 

So I wonder how I can substitute the number entered on my form for a proper MSISDN number (i.e. take the first 0 from the entered number and then prefix the string with 44)

 

Obviously if the number entered is already in a valid MSISDN format then the script needs to recognise this and not process it.

 

Any ideas if/how I can achieve the above?

 

Thanks

not too familiar with MSISDN format...but try this:

 

<?php
  $input = '01234567890';
  if(preg_match('/^(44|0)(\d{10})$/',$input,$matches)){
    $phone = '44'.$matches[2];
  }else
    die("Invalid Phone Number");
  
  echo $phone;
?>

Thanks for the replies chaps.

 

I opted for your solution radi8, just changed from:

 

<?php
$phone='011234567890';
// assumes string is proper length...
$phone= (substr($phone,0,2)!='44' ? $phone='44'.substr($phone,2,10):$phone;
?>

 

to

 

<?php
$phone='011234567890';
// assumes string is proper length...
$phone= (substr($phone,0,2)!='44' ? $phone='44'.substr($phone,1,12):$phone;
?>

 

and it worked a treat.

 

Thanks again chaps!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.