Validating phone numbers in php
Follow me as I take a look at validating user-entered data in this first installment of what I hope to be a continuing series. It’s best to use some proper PHP tools to validate your data and make sure it’s in a format your application can read before passing it along to other places.I believe that most pass off validation out of laziness, but, as you can see, validating a phone number only adds a few extra lines of code to your application.Wrap this up in a reusable function, and you have something in your PHP toolkit that can be used over and over again with ease.Likewise, you could have a free form field – rather than three separate fields – on a Web page for users to enter a phone number.This code could validate that number and parse it for your application to use.The regular expression I’m using to validate phone numbers will take a US phone number and validate it according to almost any format in current use.
There is another form of regular expression that PHP supports (POSIX extended), but I’m not going to touch on that at all.
Take a text file of customer data from an electric utility, for example.
I have no control over the phone number formats in the file, but I want to read the phone numbers, validate them against my regular expression, and break them up them into their individual components (area code, exchange, number, and extension).
Useful to state these odd cases, for when you see code that checks for an empty string and is_numeric, you can tell it's a waste of a comparison. is_numeric will also return true if the number begins with a decimal point and/or a space, provided a number follows (rather than a letter or punctuation). If we really want just an integer, this function is too broad."Source: regarding the global vs.
So, it doesn't necessarily have to start with a digit. american numeral notations, it should be noted that at least in japanese, numbers aren't grouped with an extra symbol every three digits, but rather every four digits (for example 1,0000 instead of 10.000).