Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Regular expressions Wish List | Report Bug | About Me ]

4.8.1     Basic regexps with preg_match() and preg_match_all()

This is NOT the latest copy of this book; click here for the latest version.

int preg_match ( string pattern, string subject [, array matches [, int flags [, int offset]]])

int preg_match_all ( string pattern, string subject [, array matches [, int flags [, int offset]]])

Our basic regexp function is preg_match() and takes two parameters: the pattern to match, and the string to match it against. Preg_match() will apply the regular expression in parameter one to the string in parameter two and see whether it finds a match - if it does, it will return 1, otherwise 0. The reason it returns 1 is because regular expressions return the number of matches found, but preg_match(), for speed reasons, returns as soon as it finds the first match - this means it is very quick to check whether a pattern exists in a string. An alternative function, preg_match_all(), does not exit after the first match - we will get onto that later.

Regular expressions are formed by starting with a forward slash /, followed by a sequence of special symbols and words to match, then another slash and, optionally, a string of letters that affect the expression.

Sound complicated? Don't worry - it is complicated! We're going to break it right down, though, so hopefully it will make sense immediately. Here is a list of very basic regular expressions, strings, and whether or not a match is made:

Regexp

String

Result

/php/

php

Match

php/

php

Error; you need a slash at the start

/php/

PHP

No match; regexps are case sensitive

/php/i

PHP

Match; /i means "case insensitive"

/Foo/i

FOO

Match

As you can see, "i" is the modifier to make the regexp case insensitive - it will slow things down a touch, but might make your life a great deal easier.

Here is how to use preg_match() on some of those example regexps:

<?php
    
if (preg_match("/php/", "php")) {
        print
"Got match!\n";
    }

    if (
preg_match("/php/", "PHP")) {
        print
"Got match!\n";
    }

    if (
preg_match("/php/i", "php")) {
        print
"Got match!\n";
    }
?>

The first and third preg_match() calls will return true (the second will fail because the case is wrong and "i" is not specified), and print "Got match!"





<< 4.8 Regular expressions   4.8.2 Novice regexps >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 08 Sep 2008

ghtrhghbgfhbghbghbggg

Tom - 08 Sep 2008

He did explain preg_match_all. It is the same as preg_match except it returns the number of matches.

A PHP User - 08 Sep 2008

Wait till you read page 4.8.4

Santhosh - 08 Sep 2008

I think you forgot to explain preg_match_all().



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is six plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow