Popular Posts
Tools and Tips
Search
Archives
Monday, October 15. 2007
8 Practical PHP Regular Expressions
Here are eight examples of practical PHP regular expressions and techniques that I've used over the past few years using Perl Compatible Regular Expressions. This guide goes over the eight different validation techniques and describes briefly how they work. Usernames, telephone numbers, email addresses, and more.Here are eight examples of practical PHP regular expressions and techniques that I've used over the past few years using Perl Compatible Regular Expressions. This guide goes over the eight different validation techniques and describes briefly how they work. Usernames, telephone numbers, email addresses, and more.
Thanks to Chris for pointing out that there are no US area codes below 200.
Again, whether the phone number is typed like (###) ###-####, or ###-###-#### it will validate successfully. There is also a little more leeway than specifically checking for enough numbers, because the groups of numbers can have or not have parenthesis, and be separated by a dash, period, or space.
What the ? does in this example is saying that the extra 4 digits at the end can either not exist, or exist- but only once. That way, whether or not they type them in, it will still validate correctly.
Thanks to Dave Doyle for correcting and improving the username, zip code, IP address, and date regular expressions.
These are just some examples of the Regular Expressions I've written to “get the job done” for quite awhile. They work well for the uses in which I've needed them, and hopefully they'll be of some use to you as well.
Have some regular expressions you're having a problem with? Check out our Guide for PHP Regular Expressions and PCRE Tester and Cheat Sheet. Looking for a regular expression to do something particular? Leave a comment, I'd love to hear what you have to say, and would love to hear some of your ideas for other regular expressions.

The web page hosting is done by different websites at different rates. There are few backup software that includes the facility of keeping the backup of your website data, this is done for the security reasons. For the advertisements, the famous method is the pay per click program, which enables the website to pay through click calculations. The internet connection is important while hosting a website; different wireless internet providers give better services than the dial up connection. If a data is loss, data recovery group can help in retrieving the previous data. The pay per click program is an effective internet marketing program that can be implemented to boost the visitors on page and pay only when ads are clicked.
Validating Usernames
Something often overlooked, but simple to do with a regular expression would be username validation. For example, we may want our usernames to be between 4 and 28 characters in length, alpha-numeric, and allow underscores.$string = "userNaME4234432_";
if (preg_match('/^[a-z\d_]{4,28}$/i', $string)) {
echo "example 1 successful.";
}Validating Telephone Numbers
A much more interesting example would be matching telephone numbers (US/Canada.) We'll be expecting the number to be in the following form: (###)###-####$string = "(032)555-5555";
if (preg_match('/^(\(?[2-9]{1}[0-9]{2}\)?|[0-9]{3,3}[-. ]?)[ ][0-9]{3,3}[-. ]?[0-9]{4,4}$/', $string)) {
echo "example 2 successful.";
}Thanks to Chris for pointing out that there are no US area codes below 200.
Again, whether the phone number is typed like (###) ###-####, or ###-###-#### it will validate successfully. There is also a little more leeway than specifically checking for enough numbers, because the groups of numbers can have or not have parenthesis, and be separated by a dash, period, or space.
Email Addresses
Another practical example would be an email address. This is fairly straightforward to do. There are three basic portions of an email address, the username, the @ symbol, and the domain name. The following example will check that the email address is in the valid form. We'll assume a more complicated form of email address, to make sure that it works well with even longer email addresses.$string = "first.last@domain.co.uk";
if (preg_match(
'/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',
$string)) {
echo "example 3 successful.";
}Postal Codes
Validating Postal codes (Zip codes?,) is another practical example, but is a good example to show how ? works in regular expressions.
$string = "55324-4324";
if (preg_match('/^[0-9]{5,5}([- ]?[0-9]{4,4})?$/', $string)) {
echo "example 4 successful.";
}What the ? does in this example is saying that the extra 4 digits at the end can either not exist, or exist- but only once. That way, whether or not they type them in, it will still validate correctly.
IP Addresses
Without pinging or making sure it's actually real, we can make sure that it's in the right form. We'll be expecting a normally formed IP address, such as 255.255.255.0.$string = "255.255.255.0";
if (preg_match(
'^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$',
$string)) {
echo "example 5 successful.";
}Hexadecimal Colors
Moving right along with numbers, we could check for Hexadecimal color codes, in short hand or long hand format (#333, 333, #333333 or 333333) with an optional # symbol. This could be useful in a lot of different ways... maybe previewing CSS files? Grabbing colors off pages? The options are endless.$string = "#666666";
if (preg_match('/^#(?:(?:[a-f\d]{3}){1,2})$/i', $string)) {
echo "example 6 successful.";
}Multi-line Comments
- A simple way to find or remove PHP/CSS/Other languages multi-line comments could be useful as well.$string = "/* commmmment */";
if (preg_match('/^[(/*)+.+(*/)]$/', $string)) {
echo "example 7 successful.";
}Dates
- And my last simple, yet practical example would be dates, in my favorite MM/DD/YYYY format.$string = "10/15/2007";
if (preg_match('/^\d{1,2}\/\d{1,2}\/\d{4}$/', $string)) {
echo "example 8 successful.";
}Thanks to Dave Doyle for correcting and improving the username, zip code, IP address, and date regular expressions.
These are just some examples of the Regular Expressions I've written to “get the job done” for quite awhile. They work well for the uses in which I've needed them, and hopefully they'll be of some use to you as well.
Have some regular expressions you're having a problem with? Check out our Guide for PHP Regular Expressions and PCRE Tester and Cheat Sheet. Looking for a regular expression to do something particular? Leave a comment, I'd love to hear what you have to say, and would love to hear some of your ideas for other regular expressions.

The web page hosting is done by different websites at different rates. There are few backup software that includes the facility of keeping the backup of your website data, this is done for the security reasons. For the advertisements, the famous method is the pay per click program, which enables the website to pay through click calculations. The internet connection is important while hosting a website; different wireless internet providers give better services than the dial up connection. If a data is loss, data recovery group can help in retrieving the previous data. The pay per click program is an effective internet marketing program that can be implemented to boost the visitors on page and pay only when ads are clicked.

Tracked: Oct 16, 23:25
En Devolio.com el autor hizo una lista de 8 expresiones regurales que considera útiles a la hora de programar con PHP, muchas de ellas utilizadas para validar nombres de usuario, números telefónicos, direcciones de email entre otras. Validar un nomb...
Tracked: Oct 19, 17:59
Every once in a while it's nice to drop a few links for our developer friends - just to keep them on...
Tracked: Jan 11, 17:55
Las expresiones regulares son una manera de describir un patr?n, que en PHP se puede usar para comparar, examinar o editar strings de manera pr?ctica. Las m?s conocidas son preg_match(), preg_replace() y preg_split(). Si sabemos usarlas correctamente podremos, por ejemplo, validar nombres de usuarios, n?meros de tel?fono o e-mails entre otras cosas. ?Conozc?moslas en profundidad!
Tracked: Jul 02, 15:56