PHP
  Home arrow PHP arrow Page 4 - 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 
Sun Developer Network 
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: 5 stars5 stars5 stars5 stars5 stars / 19
    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 - The regular expression syntax (contd.)


    (Page 4 of 6 )

    The space character

    To match the space character in a search string, we use the predefined Posix class, [[:space:]]. The square brackets indicate a related set of sequential characters, and ":space:" is the actual class to match (which, in this case, is any white space character). White spaces include the tab character, the new line character, and the space character. Alternatively, you could use one space character (" ") if the search string must contain just one space and not a tab or new line character. In most circumstances I prefer to use ":space:" because it signifies my intentions a bit better than a single space character, which can easy be overlooked. There are several Posix-standard predefined classes that we can match as part of a regular expression, including [:alnum:], [:digit:], [:lower:], etc. A complete list is available here.

    We can match a single space character like this:

    <?php echo ereg("Mitchell[[:space:]]Harper", "Mitchell Harper"); ?>

    We could also tell the regular expression engine to match either no spaces or one space by using the ? character after the expression, like this:

    <?php echo ereg("Mitchell[[:space:]]?Harper", "MitchellHarper"); ?>

    Grouping patterns

    Related patterns can be grouped together between square brackets. It's really easy to specify that a lower case only or upper case only sequence of characters should exist as part of the search string using [a-z] and [A-Z], like this:

    <?php

    // Require all lower case characters from first to last

    echo ereg("^[a-z]+$", "johndoe"); // Returns true

    ?>


    or like this:

    <?php

    // Require all upper case characters from first to last

    ereg("^[A-Z]+$", "JOHNDOE"); // Returns true?

    >


    We can also tell the regular expression engine that we expect either lower case or upper case characters. We do this by joining the [a-z] and [A-Z] patterns:

    <?php echo ereg("^[a-zA-Z]+$", "JohnDoe"); ?>

    In the example above, it would make sense if we could match "John Doe," and not "JohnDoe." We can use the following regular expression to do so:

    ^[a-zA-Z]+[[:space:]]{1}[a-zA-Z]+$

    It's just as easy to search for a numerical string of characters:

    <?php echo ereg("^[0-9]+$", "12345"); ?>

    Grouping terms

    It's not only search patterns that can be grouped together. We can also group related search terms together using parentheses:

    <?php echo ereg("^(John|Jane).+$", "John Doe"); ?>

    In the example above, we have a beginning of string character, followed by "John" or "Jane", at least one other character, and then the end of string character. So ...

    <?php echo ereg("^(John|Jane).+$", "Jane Doe"); ?>

    ... would also match our search pattern.

    Special character circumstances

    Because several characters are used to actually specify the grouping or syntax of a search pattern, such as the parentheses in (John|Jane), we need a way to tell the regular expression engine to ignore these characters and to process them as if they were part of the string being searched and not part of the search expression. The method we use to do this is called "character escaping" and involves propending any "special symbols" with a backslash. So, for example, if I wanted to include the or symbol '|' in my search, then I could do so like this:

    <?php echo ereg("^[a-zA-z]+\|[a-zA-z]+$", "John|Jane"); ?>

    There are only a handful of symbols that you have to escape. You must escape ^, $, (, ), ., [, |, *, ?, +, \ and {.

    Hopefully you've now gotten a bit of a feel for just how powerful regular expressions actually are. Let's now take a look at two examples of using regular expressions to validate a string of data.

    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-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT