A regular expression is a specially formatted pattern that can be used to find instances of one string in another. Several programming languages including Visual Basic, Perl, JavaScript and PHP support regular expressions, and hopefully by the end of this primer, Mitchell should have you implementing some basic regular expression functionality into your PHP pages.
PHP and Regular Expressions 101 - Regular expression examples (Page 5 of 6 )
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:
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: