Home arrow PHP arrow Page 4 - PHP and Regular Expressions 101
PHP

PHP and Regular Expressions 101


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, Mitchell should have you implementing some basic regular expression functionality into your PHP pages.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 24
March 28, 2002
TABLE OF CONTENTS:
  1. · PHP and Regular Expressions 101
  2. · What is a regular expression?
  3. · The regular expression syntax
  4. · The regular expression syntax (contd.)
  5. · Regular expression examples
  6. · Conclusion

print this article
SEARCH DEVARTICLES

PHP and Regular Expressions 101 - The regular expression syntax (contd.)
(Page 4 of 6 )

The space character

To match the space character in a search string, we use the predefined Posix class, [[:space:]]. The square brackets indicate a related set of sequential characters, and ":space:" is the actual class to match (which, in this case, is any white space character). White spaces include the tab character, the new line character, and the space character. Alternatively, you could use one space character (" ") if the search string must contain just one space and not a tab or new line character. In most circumstances I prefer to use ":space:" because it signifies my intentions a bit better than a single space character, which can easy be overlooked. There are several Posix-standard predefined classes that we can match as part of a regular expression, including [:alnum:], [:digit:], [:lower:], etc. A complete list is available here.

We can match a single space character like this:

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

We could also tell the regular expression engine to match either no spaces or one space by using the ? character after the expression, like this:

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

Grouping patterns

Related patterns can be grouped together between square brackets. It's really easy to specify that a lower case only or upper case only sequence of characters should exist as part of the search string using [a-z] and [A-Z], like this:

<?php

// Require all lower case characters from first to last

echo ereg("^[a-z]+$", "johndoe"); // Returns true

?>


or like this:

<?php

// Require all upper case characters from first to last

ereg("^[A-Z]+$", "JOHNDOE"); // Returns true?

>


We can also tell the regular expression engine that we expect either lower case or upper case characters. We do this by joining the [a-z] and [A-Z] patterns:

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

In the example above, it would make sense if we could match "John Doe," and not "JohnDoe." We can use the following regular expression to do so:

^[a-zA-Z]+[[:space:]]{1}[a-zA-Z]+$

It's just as easy to search for a numerical string of characters:

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

Grouping terms

It's not only search patterns that can be grouped together. We can also group related search terms together using parentheses:

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

In the example above, we have a beginning of string character, followed by "John" or "Jane", at least one other character, and then the end of string character. So ...

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

... would also match our search pattern.

Special character circumstances

Because several characters are used to actually specify the grouping or syntax of a search pattern, such as the parentheses in (John|Jane), we need a way to tell the regular expression engine to ignore these characters and to process them as if they were part of the string being searched and not part of the search expression. The method we use to do this is called "character escaping" and involves propending any "special symbols" with a backslash. So, for example, if I wanted to include the or symbol '|' in my search, then I could do so like this:

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

There are only a handful of symbols that you have to escape. You must escape ^, $, (, ), ., [, |, *, ?, +, \ and {.

Hopefully you've now gotten a bit of a feel for just how powerful regular expressions actually are. Let's now take a look at two examples of using regular expressions to validate a string of data.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials