WebReference.com - PHP and Regular Expressions 101 (5/5) | WebReference

WebReference.com - PHP and Regular Expressions 101 (5/5)

To page 1To page 2To page 3To page 4current_page
[previous]

PHP and Regular Expressions 101

Regular expression examples

Example One

Let's keep the first example fairly simple, and validate a standard URL. A standard URL (with no port number) consists of three parts:

[protocol]://[domain name]

Let's start by matching the protocol part of the URL. Let's make it so that only http or ftp can be used. We would use the following regular expression to do so:

^(http|ftp)

The ^ character specifies the beginning of the string, and by enclosing http and ftp in brackets and separating them with the or character (|), we are telling the regular expression engine that either the characters http or ftp must be at the beginning of the string.

A domain name usually consists of www.somesite.com, but can optionally be specified without the www part. To keep our example simple, we will only allow .com, .net, and .org domain names to be considered valid. We'd represent the search term for the domain name part of our regular expression like this:

(www\.)?.+\.(com|net|org)$

Putting everything together, our regular expression could be used to validate a domain name, like this:

<?php
  function isValidDomain($domainName)
  {
    return ereg("^(http|ftp)://(www\.)?.+\.(com|net|org)$", $domainName);
  }
  //true
  echo isValidDomain("http://www.somesite.com");
  //true
  echo isValidDomain("ftp://somesite.com");
  //false
  echo isValidDomain("ftp://www.somesite.fr");
  //false
  echo isValidDomain("www.somesite.com");
?>

Example Two

Because I reside in Sydney Australia, let's validate a typical international Australian phone number. The format of an international Australian phone number looks like this:

+61x xxxx-xxxx

The first 'x' is the area code, and the rest is the phone number. To validate that the start of the phone number is '+61' and that it is followed by an area code between two and nine, we use the following regular expression:

^\+61[2-9][[:space:]]

Notice in the search pattern above that the '\' escapes the '+' symbol so that it is included in the search and not interpreted as a regular expression. The [2-9] tells the regular expression engine that we require just one digit between two and nine inclusively. The [[:space:]] class tells the regular expression engine to expect one space in this spot.

Here's the search pattern for the rest of the phone number:

[0-9]{4}-[0-9]{4}$

Nothing out of the ordinary here, we're just telling the regular expression engine that for the phone number to be valid, it must have a group of four digits, followed by a hyphen, followed by another group of four digits and then an end of string character.

Putting the entire regular expreesion together into a function, we can use the code to validate some international Australian phone numbers:

<?php
  function isValidPhone($phoneNum)
  {
    echo ereg("^\+61[2-9][[:space:]][0-9]{4}-[0-9]{4}$", $phoneNum);
  }
  // true
  echo isValidPhone("+612 9555-5555");
  // false
  echo isValidPhone("+61 95555555");
  // false
  echo isValidPhone("+611 9555-5555");
?>

Conclusion

Regular expressions take a lot of the hassle out of writing lines and lines of repetitive code to validate a string. Over the last few pages we've covered all of the basics of Posix standard regular expression patterns including symbols, grouping and the PHP ereg function. We've also seen how to use the regular expression engine to validate some simple strings in PHP.

About the Author

Mitchell Harper is the founder and senior editor of DevArticles.com. DevArticles provides its visitors with daily programming articles, tutorials, news, reviews, interviews, and free programming eBook links. If you're currently working with ASP, PHP, C#, VB, HTML, JavaScript, MySQL or SQL Server, then be sure to visit http://www.devarticles.com.


To page 1To page 2To page 3To page 4current_page
[previous]

Created: March 4, 2002
Revised: March 4, 2002

URL: http://webreference.com/programming/php/regexps/5.html