PHP regular expression tutorial


Home - Tutorials - Basics

In this article I will show you how to use PHP regular expressions in your own PHP scripts.

Tutorial info:


Name:PHP regular expression tutorial
Total steps:2
Category:Basics
Level:Beginner

Bookmark PHP regular expression tutorial



Bookmark and Share

Step 1 - Regular expressions basics


PHP regular expression tutorial

PHP regular expressions seems to be a quite complicated area especially if you are not an experienced Unix user. Historically regular expressions were originally designed to help working with strings under Unix systems.

Using regular expressions you can easy find a pattern in a string and/or replace it if you want. This is a very powerful tool in your hand, but be careful as it is slower than the standard string manipulation functions. 

Regular expression types 

There are 2 types of  regular expressions:

The ereg, eregi, ... are the POSIX versions and preg_match, preg_replace, ... are the Perl version. It is important that using Perl compatible regular expressions the expression should be enclosed in the delimiters, a forward slash (/), for example. However this version is more powerful and faster as well than the POSIX one.

The regular expressions basic syntax

To use regular expressions first you need to learn the syntax of the patterns. We can group the characters inside a pattern like this:

An example pattern to check valid emails looks like this:

Code:
^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$

The code to check the email using Perl compatible regular expression looks like this:

Code:
  1. $pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
  2. $email = "jim@demo.com";
  3.  
  4. if (preg_match($pattern,$email)) echo "Match";
  5. else echo "Not match";

And very similar in case of POSIX extended regular expressions:

Code:
  1. $pattern = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$";
  2. $email = "jim@demo.com";
  3.  
  4. if (eregi($pattern,$email)) echo "Match";
  5. else echo "Not match";

 

Now let's see a detailed pattern syntax reference:

Regular expression (pattern)

Match (subject)

Not match (subject)

Comment
world Hello world Hello Jim Match if the pattern is present anywhere in the subject
^world world class Hello world Match if the pattern is present at the beginning of the subject
world$ Hello world world class Match if the pattern is present at the end of the subject
world/i This WoRLd Hello Jim Makes a search in case insensitive mode
^world$ world Hello world The string contains only the "world"
world* worl, world, worlddd wor There is 0 or more "d" after "worl"
world+ world, worlddd worl There is at least 1 "d" after "worl"
world? worl, world, worly wor, wory There is 0 or 1 "d" after "worl"
world{1} world worly There is 1 "d" after "worl"
world{1,} world, worlddd worly There is 1 ore more "d" after "worl"
world{2,3} worldd, worlddd world There are 2 or 3 "d" after "worl"
wo(rld)* wo, world, worldold wa There is 0 or more "rld" after "wo"
earth|world earth, world sun The string contains the "earth" or the "world"
w.rld world, wwrld wrld Any character in place of the dot.
^.{5}$ world, earth sun A string with exactly 5 characters
[abc] abc, bbaccc sun There is an "a" or "b" or "c" in the string
[a-z] world WORLD There are any lowercase letter in the string
[a-zA-Z] world, WORLD, Worl12 123 There are any lower- or uppercase letter in the string
[^wW] earth w, W The actual character can not be a "w" or "W"

 

On the next page you will find some more complex regular expression with detailed explanation. 





Next Step of PHP regular expression tutorial


Tags: php regular expression, regular expression, php



Follow phpf1 on Twitter




F1 Site Family
AJAX F1
CSS F1
Database F1
Flash F1
HTML F1
Java F1
JavaScript F1
PhotoShop F1
PHP F1
Scripts F1
Tutorial F1
Windows F1

Total time: 0.0448