Jump to content

working with phone numbers


CincoPistolero

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.