Posted by: phillipnb | March 11, 2011

php and regular expressions – part 1

Many folks consider PHP as a language for the weak and destitute. They say that php is easy to learn and implement. Honestly, the truth is far away from these kind of small talk. The same folks who claim that php is a silly language are not ready to face the php certification or work on a full fledged php application. They are neither ready to say what it is that php cannot do compared to other web programming languages nor do they know what the strengths of php are. Of course a language’s ability is not measured in its users ability to pass a certification or write a full fledged application. It is also true that php is not c plus plus or java, but php has its own place in the world of programming languages.

One of the things that php can be proud of is its support for perl compatible regular expression. Without PCRE, php could have been thought of as the language of beggars and vagabonds of programming. That is not the case, php is powerful when it comes to working with regular expressions and pattern matching. Combined with php’s ability to deliver object oriented code, PCRE becomes an ideal tool in the hands of a php developer who has set out to find a particular string from thousands and thousands of lines of text.

So, what is php’s role with regular expressions and pattern matching? PHP’s power of regular expression can be felt when you really do not know the exact string that you want to match. Of course I consider PERL as the king in such situations but PHP is not far behind. Before I tell you how to use php with regular expressions, I need to explain a few terms like expression, subexpression, delimiter, metacharacter, quantifier etc. Once you understand these terms and the rules associated with each one of them, you are all set to write patterns using regular expression to match any string that you can possibly think of.


A regular expression must always have a starting and ending character to show the boundary of that expression. To achieve this, always pick a character which is unlikely to appear in the expression/pattern itself. if it occurs, you need to escape this character (some times this can be confusing). A good example for delimiter is forward slash ‘/’


A set of rules with a well defined delimiter. e.g. /abc/

Sub Expression

The expression that is contained within the main regular expression or another sub expression is called a sub expression. e.g. ab(cdf)


A metacharacter usually represents a single character in the matched string but sometimes based on their type, metacharacters can represent many characters.


‘.’ means match any character

‘^’ means start the matching process at the beginning of the string

‘$’ means start the matching process at the end of the string

‘\s’ means match any character which is a white space

‘\d’ means match any number/digit

‘\w’ means match any word

‘[]’ match alternatives within the square brackets


A quantifier tells the number of times a particular character/metacharacter can appear in a matched string


* means zero/more times
+ means one/more times
? means zero/one time
{n,m} the range with ‘n’ denoting the minimum number of times and ‘m’ the maximum times

So far we discussed about the rules to write a pattern using regular expressions. Now, we need to find a function that uses these regular expression pattern to find matching strings. Fortunately, php has built-in functions like preg_match() and preg_match_all()

In the next edition of this article we will work on a few examples using the regular expression rules that we have learned here.

Till then, Happy PHPing



  1. more easily examples

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: