Home arrow PHP arrow Page 5 - PHP and Regular Expressions 101
PHP

PHP and Regular Expressions 101


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.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 24
March 28, 2002
TABLE OF CONTENTS:
  1. · PHP and Regular Expressions 101
  2. · What is a regular expression?
  3. · The regular expression syntax
  4. · The regular expression syntax (contd.)
  5. · Regular expression examples
  6. · Conclusion

print this article
SEARCH DEVARTICLES

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:

<?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("+619 0000-0000");

// false

echo isValidPhone("+61 00000000");

// false

echo isValidPhone("+611 00000000");

?>

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials