Home arrow PHP arrow Page 3 - 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
(Page 3 of 6 )

Before you can match a string to a regular expression, you have to create the regular expression. The syntax of regular expressions is a little quirky at first, and each phrase in the expression represents some sort of search criteria. Here's a list of some of the most common regular expressions, as well as an example of how to use each one:

Beginning of string:

To search from the beginning of a string, use ^. For example,

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

Would return true, however

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

would return false, because hello wasn't at the beginning of the string.

End of string:

To search at the end of a string, use $. For example,

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

Would return true, however

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

would return false, because bye wasn't at the very end of the string.

Any single character:

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

<?php echo ereg(".", "cat"); ?>

would return true, however

<?php echo ereg(".", ""); ?>

would return false, because our search string contains no characters. You can optionally tell the regular expression engine how many single characters it should match using curly braces. If I wanted a match on five characters only, then I would use ereg like this:

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

The code above tells the regular expression engine to return true if and only if at least five successive characters appear at the end of the string. We can also limit the number of characters that can appear in successive order:

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

In the example above, we have told the regular expression engine that in order for our search string to match the expression, it should have between one and three 'a' characters at the end.

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

The example above wouldn't return true, because there are three 'a' characters in the search string, however they are not at the end of the string. If we took the end-of-string match $ out of the regular expression, then the string would match.

We can also tell the regular expression engine to match at least a certain amount of characters in a row, and more if they exist. We can do so like this:

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

Repeat character zero or more times

To tell the regular expression engine that a character may exist, and can be repeated, we use the * character. Here are two examples that would return true:

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

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


Even though the second example doesn't contain the 't' character, it still returns true because the * indicates that the character may appear, and that it doesn't have to. In fact, any normal string pattern would cause the second call to ereg above to return true, because the 't' character is optional.

Repeat character one or more times

To tell the regular expression engine that a character must exist and that it can be repeated more than once, we use the + character, like this:

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

The following example would also return true:

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

Repeat character zero or one times

We can also tell the regular expression engine that a character must either exist just once, or not at all. We use the ? character to do so, like this:

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

If we wanted to, we could even entirely remove the 'c' from the search string shown above, and this expression would still return true. The '?' means that a 'c' may appear anywhere in the search string, but doesn't have to.
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