Web Development

  Homes arrow Web Development arrow PHP and regular expressions 101 Part 2
 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 2
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 expressionsyntax

    Beforeyou can match a string toa regular expression, youhave to create the regularexpression. The syntax ofregular expressions is alittle quirky at first,and each phrase in the expressionrepresents some sort ofsearch criteria. Here'sa list of some of the mostcommon regular expressions,as well as an example ofhow to use each one:

    Beginningof string:
    To search from the beginningof a string, use ^. Forexample,

    <?phpecho ereg("^hello","hello world!");?>

    Wouldreturn true, however

    <?phpecho ereg("^hello","i say hello world");?>

    wouldreturn false, because hellowasn't at the beginningof the string.

    Endof string:
    To search at the end ofa string, use $. For example,

    <?phpecho ereg("bye$","goodbye"); ?>

    Wouldreturn true, however

    <?phpecho ereg("bye$","goodbye my friend");?>

    wouldreturn false, because byewasn't at the very end ofthe string.

    Anysingle character:
    To search for any character,use the dot. For example,

    <?phpecho ereg(".","cat"); ?>

    wouldreturn true, however

    <?phpecho ereg(".",""); ?>

    wouldreturn false, because oursearch string contains nocharacters. You can optionallytell the regular expressionengine how many single charactersit should match using curlybraces. If I wanted a matchon five characters only,then I would use ereg likethis:

    <?phpecho ereg(".{5}$","12345"); ?>

    Thecode above tells the regularexpression engine to returntrue if and only if at leastfive successive charactersappear at the end of thestring. We can also limitthe number of charactersthat can appear in successiveorder:

    <?phpecho ereg("a{1,3}$","aaa"); ?>

    Inthe example above, we havetold the regular expressionengine that in order forour search string to matchthe expression, it shouldhave between one and three'a' characters at the end.

    <?phpecho ereg("a{1,3}$","aaab"); ?>

    Theexample above wouldn't returntrue, because there arethree 'a' characters inthe search string, howeverthey are not at the endof the string. If we tookthe end-of-string match$ out of the regular expression,then the string would match.
    We can also tell the regularexpression engine to matchat least a certain amountof characters in a row,and more if they exist.We can do so like this:

    <?phpecho ereg("a{3,}$","aaaa"); ?>

    Repeatcharacter zero or more times
    To tell the regular expressionengine that a charactermay exist, and can be repeated,we use the * character.Here are two examples thatwould return true:

    <?phpecho ereg("t*","tom"); ?>
    <?php echo ereg("t*","fom"); ?>

    Eventhough the second exampledoesn't contain the 't'character, it still returnstrue because the * indicatesthat the character may appear,and that it doesn't haveto. In fact, any normalstring pattern would causethe second call to eregabove to return true, becausethe 't' character is optional.

    Repeatcharacter one or more times

    Totell the regular expressionengine that a charactermust exist and that it canbe repeated more than once,we use the + character,like this:

    <?phpecho ereg("z+","i like the zoo");?>

    Thefollowing example wouldalso return true:

    <?phpecho ereg("z+","i like the zzzzzzoo!");?>

    Repeatcharacter zero or one times
    We can also tell the regularexpression engine that acharacter must either existjust once, or not at all.We use the ? character todo so, like this:

    <?phpecho ereg("c?","cats are fuzzy");?>

    Ifwe wanted to, we could evenentirely remove the 'c'from the search string shownabove, and this expressionwould still return true.The '?' means that a 'c'may appear anywherein the search string, butdoesn't have to.

    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