Home > > Coreg

Split Phone Number with PHP? (6)


01-27-2013 01:56 PM #1 pain2k (Veteran Member)
Split Phone Number with PHP?

How do you split a phone number input with the explode function if you are there's no separator like in this case 555-555-5555 where is a dash as opposed to 5555555555 where there's none?

I assume you would count the characters then explode?


01-27-2013 09:53 PM #2 snipe (Member)

I'd just use the substr function!

[php]
$num = 5556667777;
$one=substr($num,0,3); $two=substr($num,3,3); $three=substr($num,6,4);
$formated = $one.'-'.$two.'-'.$three;
echo $formated;
[/php]


01-27-2013 10:08 PM #3 thomasbhm (Member)

I would use JS to handle the formatting and the double check the result w/ php. That way if they do put the dash in you can make sure it's formatted properly before it's submitted. I'm sure there are a million and one code snippets in google to help you with this. It's very common.


01-28-2013 01:27 AM #4 pain2k (Veteran Member)

ok thanks for the suggestions.


01-28-2013 02:44 AM #5 thomasbhm (Member)

no problem... here is on w/ jquery which is what I prefer:

http://stackoverflow.com/questions/8...-number-format

stack overflow is a great place to find this kind of stuff by the way.


01-28-2013 02:51 AM #6 pain2k (Veteran Member)

Yeah love stackoverflow. Thanks for that link. Nice example.


Home > > Coreg