Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

How to use regular expressions in PHP

Validate user input, parse user input and file contents, and reformat strings

Nathan A. Good, Consultant, Alliance of Computer Professionals
Nathan A. Good is an author, software engineer, and system administrator in the Twin Cities in Minnesota. His books include PHP 5 Recipes: A Problem-Solution Approach by Lee Babin, et al (Apress, 2005), Regular Expression Recipes for Windows Developers: A Problem-Solution Approach (Apress, 2005), Regular Expressions: A Problem-Solution Approach (Apress, 2005), and Professional Red Hat Enterprise Linux 3 by Kapil Sharma et al (Wrox, 2004).

Summary:  Regular expressions can provide a powerful way to work with text. Using regular expressions, you can do complex validation of user input, parse user input and file contents, and reformat strings. PHP provides simple methods that let you use POSIX and PCRE regular expressions. This tutorial discusses the differences between POSIX and PCRE, and how you can use regular expressions and PHP V5.

Date:  10 Jan 2006
Level:  Intermediate PDF:  A4 and Letter (36 KB | 11 pages)Get Adobe® Reader®

Activity:  18845 views
Comments:  

POSIX replacements

The methods ereg_replace() and eregi_replace() make replacements in text and feature POSIX regexes.

ereg_replace()

You can use the ereg_replace() method to make case-sensitive replacements in POSIX regex syntax. The following example demonstrates how to replace an e-mail address in a string with a hyperlink:


Listing 3. ereg_replace() method
                    
<?php
$origstr = "My e-mail address is: first.last@example.com";
// Syntax is: ereg_replace( regex, replacestr, string )
$newstr = \
ereg_replace("([.[:alpha:][:digit:]]+@[.[:alpha:][:digit:]]+)", 
    "<a href=\"mailto:\\1\">\\1</a>", $origstr);
print("$newstr\n");
?>

This is an incomplete version of a regex to match e-mail addresses, but it shows that ereg_replace() is more powerful than a regular replacement function like str_replace(). When you use a regex, you define rules by which to do searches, instead of searching for literal characters.

eregi_replace()

With the exception of ignoring case, the eregi_replace() function is identical to ereg_replace():


Listing 4. eregi_replace() function
                    
<?php
$origstr = "1 BANANA, 2 banana, 3 Banana";
// Syntax is: eregi_replace( regex, replacestr, string )
$newstr = eregi_replace("banana", "pear", $origstr);
print("New string is: '$newstr'\n");
?>

This example replaces banana with pear, regardless of case.

6 of 11 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source
ArticleID=133632
TutorialTitle=How to use regular expressions in PHP
publish-date=01102006
author1-email=mail@nathanagood.com
author1-email-cc=