spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / php / regexps current pageTo page 2To page 3To page 4To page 5
[next]

PHP and Regular Expressions 101

Developer News
MicrosoftÂ’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads For The JON 2.0

By Mitchell Harper (devart@devarticles.com)

Introduction

Regular expressions are one of those quirky features that popup in a variety of programming languages, but because they seem to be a difficult concept to grasp, many developers push them away into the corner, forgetting that they even exist.

A regular expression is a specially formatted pattern that can be used to find instances of one string in another. Several programming languages including Visual Basic, Perl, JavaScript and PHP support regular expressions, and hopefully by the end of this primer you should be able to implement some basic regular expression functionality into your PHP pages.

Let's start by taking a look at what a regular expression is, and why you'd want to use them in your PHP pages.

What is a regular expression?

What do you think it is that separates programs like BBEdit and notepad from the good old console-based text editors? Both support text input and let you save that text into a file, however modern text editors also support other functionality including find-replace tools, which makes editing a text file that much easier.

Regular expressions are similar, only better. Think of a regular expression as an extremely advanced find-replace tool that saves us the pain of having to write custom data validation routines to check e-mail addresses, make sure phone numbers are in the correct format, etc.

One of the most common functions of any program is data validation, and PHP comes bundled with several text validation functions that allow us to match a string using regular expressions, making sure there's a space here, a question mark there, etc.

What you may not know however, is that regular expressions are simple to implement, and once you've mastered a few regular expressions (which are specially formatted strings that we can use to tell the regular expression engine the portion of a string we want to match) you'll be asking yourself why you left regular expressions in the corner for so long.

Note: PHP has two sets of functions for dealing with the two types of regular expression patterns: Perl 5 compatible patterns, and Posix standard compatible patterns. In this article we will be looking at the ereg function and working with search expressions that conform to the Posix standard. Although they don't offer as much power as Perl 5 patterns, they're a great way to start learning regular expressions. If you're interested in PHP's support for Perl 5 compatible regular expressions, then see the PHP.net site for details on the preg set of PHP functions.

PHP has six functions that work with regular expressions. They all take a regular expression string as their first argument, and are shown below:


home / programming / php / regexps current pageTo page 2To page 3To page 4To page 5
[next]
click here



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Perl Pragma Primer · Implement Drag and Drop in Your Web Apps: Part 2 · How to Create an Ajax Autocomplete Text Field: Part 5
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
SQL Server 2005 Express Edition - Part 22 - Upgrading from Microsoft SQL Server Desktop Engine (MSDE) · Vyatta: Downgrades that Pay Off · NetMotion Brings Cross-Network Support to Wireless VoIP

Created: March 4, 2002
Revised: March 4, 2002

URL: http://webreference.com/programming/php/regexps/