• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Learn regular expressions in PHP

By Akash Mehta | on Apr 30, 2008 | 1 Comment
PHP Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

Love them or hate them, regular expressions are here to stay. When it comes to quickly dealing with large blocks of data, batch processing operations or screen scraping, regular expressions are often the most effective solution. There’s just one problem, though – learning them can be as hard as learning a new language altogether. Here’s how to get off to a flying start.

First, you need to how regular expressions work. Stick with PCRE – the preg_* functions – they’re faster, more reliable and expressions for them are more common. The different styles of regular expressions each define a mini language for matching patterns within a block of text. Screen scraping is probably the most popular example. Consider this:

1
Not 1.
Yet another link.

What if you wanted to programmatically extract the link URLs and text from each of these? At this level of simplicty, some basic explode()s might be enough, and when you need to be more tolerant for the data, XML parsing is probably the way to go. But for now, just looking at that, regular expressions would be the easiest way. And we can build a pattern that defines what is to be matched so that the regular expression engine (in this case, PCRE) will find all matches and give them to us.

One of the best ways to understand regular expressions in practise, I find, is to consider mod_rewrite. You’ve seen those fantastic URLs on websites – e.g. http://example.com/users/myusername – these don’t actually exist as a file, but we can use Apache’s mod_rewrite engine to “rewrite” these URLs to other URLs. We might make that /users/(some username) go to /users.php?username=(the username). The best tutorial for this, by far, is the corz.org htaccess tips and tricks guide. Have a very thorough read through this and experiment with it extensively before you go on.

Next, get equipped to test out regular expressions. You’re probably using Firefox. If you aren’t, get it. Then get the Regular Expressions Tester Firefox extension. This will be invaluable for experimenting with regular expressions – the best way to learn.

Of course, you’ll need to be familiar with the PHP functions that will provide your regular expression workhorse. Bookmark the preg_match function’s manual entry and refer back to it as you need to. The manual page has some invaluable examples which you should read through extensively.

Finally, when in doubt, keep a cheatsheet at hand. ILoveJackDaniels.com has a fantastic regular expression cheatsheet that you should also bookmark.

With all these ready, you’ll be off to a great start. Remember, the best way to learn is to practise — read through real, working code, then write your own. Once you’ve finished all this, you’ll be a regular expression whiz in no time.

Share this story:
  • tweet

Tags: PHP Tutorialsregular expressions

Author Description

One Response to “Learn regular expressions in PHP”

  1. February 22, 2009

    Mexabet Log in to Reply

    Interesting article on regular expressions.

    Note: The site ILoveJackDaniels.com doesn’t exist anymore. It has been moved to addedbytes.com and this new site contains the original regular expressions cheat sheet. So, you may want to update your links.

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 1,241Followers 493Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.