PHP
  Home arrow PHP arrow Page 5 - PHP and Regular Expressions 101
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP and Regular Expressions 101
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2002-03-28

    Table of Contents:
  • PHP and Regular Expressions 101
  • What is a regular expression?
  • The regular expression syntax
  • The regular expression syntax (contd.)
  • Regular expression examples
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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");

    ?>

    More PHP Articles
    More By Mitchell Harper


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT