Bricktop Posted April 24, 2009 Share Posted April 24, 2009 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 Link to comment https://forums.phpfreaks.com/topic/155493-solved-remove-first-character-and-replace-with-44/ Share on other sites More sharing options...
rhodesa Posted April 24, 2009 Share Posted April 24, 2009 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; ?> Link to comment https://forums.phpfreaks.com/topic/155493-solved-remove-first-character-and-replace-with-44/#findComment-818207 Share on other sites More sharing options...
radi8 Posted April 24, 2009 Share Posted April 24, 2009 Well, you can try this: <?php $phone='011234567890'; // assumes string is proper length... $phone= (substr($phone,0,2)!='44' ? $phone='44'.substr($phone,2,10):$phone; ?> That should get you close Link to comment https://forums.phpfreaks.com/topic/155493-solved-remove-first-character-and-replace-with-44/#findComment-818219 Share on other sites More sharing options...
Bricktop Posted April 24, 2009 Author Share Posted April 24, 2009 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! Link to comment https://forums.phpfreaks.com/topic/155493-solved-remove-first-character-and-replace-with-44/#findComment-818235 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.