CincoPistolero Posted November 20, 2009 Share Posted November 20, 2009 I'm trying to only input numbers into the mysql DB. So when someone does this 333-3333 or 333.3333 I"m trying to strip out anything but the numbers. Below is the code I'm using $phoneNum= preg_replace("[^0-9]", "", $phone); This however, is not removing the dots or dashes. Any clues as to how to do this Quote Link to comment https://forums.phpfreaks.com/topic/182248-working-with-phone-numbers/ Share on other sites More sharing options...
Alex Posted November 20, 2009 Share Posted November 20, 2009 You need delimiters. $phoneNum= preg_replace("~[^0-9]~", "", $phone); Quote Link to comment https://forums.phpfreaks.com/topic/182248-working-with-phone-numbers/#findComment-961681 Share on other sites More sharing options...
CincoPistolero Posted November 20, 2009 Author Share Posted November 20, 2009 How about if user uses a space like this 444 4444. When I error check, the below fails if (!preg_match('~^[0-9.-[:SPACE:]]+$~',$_POST['phone'])) { It does not like the space Quote Link to comment https://forums.phpfreaks.com/topic/182248-working-with-phone-numbers/#findComment-961700 Share on other sites More sharing options...
ngreenwood6 Posted November 20, 2009 Share Posted November 20, 2009 or you could use str_replace, ex: $string_one = '888-888-1099'; $string_two = '888.888.1099'; $replace = array('-','.'); $new_string_one = str_replace('',$replace,$string_one); $new_string_two = str_replace('',$replace,$sting_two); Both of those will output 8888881099. Just add whatever characters you want removed to the $replace array. Quote Link to comment https://forums.phpfreaks.com/topic/182248-working-with-phone-numbers/#findComment-961701 Share on other sites More sharing options...
ngreenwood6 Posted November 20, 2009 Share Posted November 20, 2009 just saw your post about the space you can add a space to the array if you use str_replace: $replace = array(' ','-','.'); Quote Link to comment https://forums.phpfreaks.com/topic/182248-working-with-phone-numbers/#findComment-961703 Share on other sites More sharing options...
CincoPistolero Posted November 20, 2009 Author Share Posted November 20, 2009 I want the regex to see the space and not error on it. I'll then remove it later in the php code. But, for now, the regex below triggers and error when it sees the space. It should see the space and not care (!preg_match('~^[0-9.-[:SPACE:]]+$~',$_POST['phone'])) Quote Link to comment https://forums.phpfreaks.com/topic/182248-working-with-phone-numbers/#findComment-961720 Share on other sites More sharing options...
Zane Posted November 20, 2009 Share Posted November 20, 2009 $str = "706.--- (555)******/////3205 . extension: 436"; echo preg_replace('#[^0-9]#','',$str); ?> Outputs: 7065553205436 Is that what your looking for? EDIT: it seems Alex beat me to it. This is basically the same as his solution. Quote Link to comment https://forums.phpfreaks.com/topic/182248-working-with-phone-numbers/#findComment-961747 Share on other sites More sharing options...
.josh Posted November 20, 2009 Share Posted November 20, 2009 How about if user uses a space like this 444 4444. When I error check, the below fails if (!preg_match('~^[0-9.-[:SPACE:]]+$~',$_POST['phone'])) { It does not like the space You do not need to add the dot or hyphen or space. Instead of trying to match possible formats, strip everything out but the numbers (using preg_replace pattern posted by alex and zanus) and then count how many numbers there are. If you expect 10 digits (U.S. area code plus number) strlen($result) == 10. If not, tell user you expect area code plus number no leading 1's etc... Quote Link to comment https://forums.phpfreaks.com/topic/182248-working-with-phone-numbers/#findComment-962016 Share on other sites More sharing options...
nrg_alpha Posted November 20, 2009 Share Posted November 20, 2009 @CincoPistolero To add to CV's comment, even if it is desired to preserve spaces, dashes and dots (which I wouldn't recommend - CV pointed out what needed to be done), note that the location of an unescaped dash is crucial within a character class. If it is not the very first or very last character (and again, we're talking about unescaped dashes here), the dash creates a range between the characters sandwitched on either side of it. So either escape it, or place it as the starting or finishing character (the latter is my personal choice). As far as the space is concerned, that is not how you would do it (you're using POSIX notation in PCRE regex).. you can either insert a literal space, or use \x20 (space represented as a hex escaped value), or use \s (which is a special shorthand character class which represents all whitespace characters). So to rewrite the above snippet, it could be something like: if (!preg_match('#^[0-9.\s-]+$#', $_POST['phone'])) { Many ways to skin a cat of course... but again, I would recommend CV's approach. No need to fuss with over complicated formatting. After stripping all non-numeric characters, if the length of numbers is correct, you can always turn around and format it to your liking afterwards. Quote Link to comment https://forums.phpfreaks.com/topic/182248-working-with-phone-numbers/#findComment-962058 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.