Page 1 of 1

Add Spaces

Posted: Mon Aug 11, 2014 4:19 pm
by donny
Hello,

I have a form and the user can add their phone number.

if they enter their phone number 1234567891 is there a way i can change it on my processing page to
123 456 7891


thank you

Re: Add Spaces

Posted: Mon Aug 11, 2014 4:28 pm
by requinix
If they enter a 10 (strlen) digit (ctype_digit) number, grab (substr) the first three characters, the second three, and the last four, and output them with spaces in between.

Code: Select all

if (strlen($number) == 10 && ctype_digit($number)) {
    $number = substr($number, 0, 3) . " " . substr($number, 3, 3) . " " . substr($number, 6);
}
You may want to do other things first like strip spaces and symbols.

Re: Add Spaces

Posted: Mon Aug 11, 2014 5:02 pm
by donny
i am having trouble understanding this .. i have a 9 digit number that i need to put a space after every 3 characters 123456789 to 123 456 789

thank you

Re: Add Spaces

Posted: Mon Aug 11, 2014 5:07 pm
by Celauran
Use the above answer, but change the strlen comparison from 10 to 9.

Re: Add Spaces

Posted: Mon Aug 11, 2014 5:49 pm
by donny
perfect thank you both for your help