ctype_digit but allow spaces

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

ctype_digit but allow spaces

Post by ed209 »

I want to validate some data - it has to be a phone number so only numbers, but I'd also like to allow spaces. can I use ctype_digit in combination with something?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Only if you remove the spaces could you use ctype_digit(). Best use a regular expression I think.
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post by ed209 »

good point, why didn't I think of that :roll: . a simple

Code: Select all

$number = str_replace(" ", "", $tel);

//then 

ctype_digit($number):
did the trick. I've yet to learn regex
Post Reply