Web Development

  Homes arrow Web Development arrow PHP and regular expressions 101 Part 3
 Webmaster Tools
Base64 Encoding 
Browser Settings 
CSS Coder 
CSS Navigation Menu 
Datetime Converter 
DHTML Tooltip 
Dig Utility 
DNS Utility 
Dropdown Menu 
Fetch Content 
Fetch Header 
Floating Layer 
htaccess Generator 
HTML Encoder 
HTML Entities 
IP Convert 
Meta Tags 
Password Encryption
Password Strength
Pattern Extractor 
Ping Utility 
Pop-Up Window 
Regex Extractor 
Regex Match 
Scrollbar Color 
Source Viewer 
Syntax Highlighting 
URL Encoding 
Web Safe Colors 
Forums Sitemap 
Weekly Newsletter
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Contact Us 
Site Map 
Privacy Policy 
  >>> SIGN UP!  
  Lost Password? 

PHP and regular expressions 101 Part 3
By: Developer Shed
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0

    Table of Contents:

    Rate this Article: Poor Best 
      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




    PHPand regular expressions101 (continued)


    The regular expression syntax

    Thespace character
    To match the space characterin a search string, we usethe predefined Posix class,[[:space:]]. The squarebrackets indicate a relatedset of sequential characters,and ":space:"is the actual class to match(which, in this case, isany white space character).White spaces include thetab character, the new linecharacter, and the spacecharacter. Alternatively,you could use one spacecharacter (" ")if the search string mustcontain just one space andnot a tab or new line character.In most circumstances Iprefer to use ":space:"because it signifies myintentions a bit betterthan a single space character,which can easy be overlooked.There are several Posix-standardpredefined classes thatwe can match as part ofa regular expression, including[:alnum:], [:digit:], [:lower:],etc. Acomplete list is availablehere.

    Wecan match a single spacecharacter like this:

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

    Wecould also tell the regularexpression engine to matcheither no spaces or onespace by using the ? characterafter the expression, likethis:

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

    Related patterns can begrouped together betweensquare brackets. It's reallyeasy to specify that a lowercase only or upper caseonly sequence of charactersshould exist as part ofthe search string using[a-z] and [A-Z], like this:


    //Require all lower case charactersfrom first to last
    echo ereg("^[a-z]+$","johndoe"); //Returns true


    orlike this:


    //Require all upper case charactersfrom first to last
    ereg("^[A-Z]+$","JOHNDOE"); //Returns true?


    Wecan also tell the regularexpression engine that weexpect either lower caseor upper case characters.We do this by joining the[a-z] and [A-Z] patterns:

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

    Inthe example above, it wouldmake sense if we could match"John Doe," andnot "JohnDoe."We can use the followingregular expression to doso:


    It'sjust as easy to search fora numerical string of characters:

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

    It's not only search patternsthat can be grouped together.We can also group relatedsearch terms together usingparentheses:

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

    Inthe example above, we havea beginning of string character,followed by "John"or "Jane", atleast one other character,and then the end of stringcharacter. So ...

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

    ...would also match our searchpattern.

    Specialcharacter circumstances
    Because several charactersare used to actually specifythe grouping or syntax ofa search pattern, such asthe parentheses in (John|Jane),we need a way to tell theregular expression engineto ignore these charactersand to process them as ifthey were part of the stringbeing searched and not partof the search expression.The method we use to dothis is called "characterescaping" and involvespropending any "specialsymbols" with a backslash.So, for example, if I wantedto include the or symbol'|' in my search, then Icould do so like this:

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

    Thereare only a handful of symbolsthat you have to escape.You must escape ^, $, (,), ., [, |, *, ?, +, \ and{.
    Hopefully you've now gottena bit of a feel for justhow powerful regular expressionsactually are. Let's nowtake a look at two examplesof using regular expressionsto validate a string ofdata.

    Topquality daily ASP, PHP and.NET
    articles, tutorials, news,reviews, interviews AND FREEEBOOKS! devArticles is
    the ultimate online resourcefor the serious web developer.Visit



    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Web Development Articles
    More By Developer Shed



    - On Page SEO for New Domains
    - Improve Your Site`s Speed
    - Safari Books Online Review
    - Creating an Estore From the Ground Up
    - Most Common SEO Mistakes Developers Make
    - Making the Most of Your Titles and Meta Desc...
    - Five Ways Using Flash Can Damage Your Site
    - A Web Designer`s Guide to Colors
    - Use Webstarts to Create a Free Site
    - More Than Just Looks. How Your Web Design C...
    - How to Design Content Pages
    - Mint Review
    - Make Your WordPress Website Look Professional
    - How to Create a Mobile Web Site
    - Meta Tags: Still Useful?

    Developer Shed Affiliates


    © 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap