Search Helium

Home > Computers & Technology > Software > Software & Web Development

Regex modifiers in PHP

by Chrys

Created on: March 05, 2009   Last Updated: March 08, 2009

Regex Modifiers in PHP PHP Regular Expressions Part V

Introduction Matching is case sensitive. You may not know if what you are looking for is in lower case or upper case or has mixed cases. It is possible for you to make a case insensitive match. You need what is called a modifier for this. There are a good number of modifiers and each has its own purpose. We shall learn some of them in this part of the series.

The i Modifier By default, matching is case sensitive. To make it case insensitive, you have to use what is called the i modifier.

So if we have the regex,

/send/

and then we also have

$subject = "Click the Send button."

the following code will not produce a match:

$subject = "Click the Send button.";

<?php $subject = "Click the Send button.";

if (preg_match("/send/", $subject)) echo "Matched" . "<br />"; else echo "Not Matched" . "<br />"; ?>

The regex did not match the subject string because the regex has "send" where S is in lower case, but the subject string has "Send" where S is in upper case. If you want this matching to be case insensitive, then your regex will have to be

/send/i

Note the i just after the second forward slash. It is the i modifier. The following code will produce a match.

<?php $subject = "Click the Send button.";

if (preg_match("/send/i", $subject)) echo "Matched" . "<br />"; else echo "Not Matched" . "<br />"; ?>

Matching has occurred because we have made the regex case insensitive, with the i modifier.

Global Matching It is possible for you to have more than one sub string in the subject string that would match the regex. By default, only the first sub string in the subject is matched. To match all the sub strings in the subject, you have to use the function preg_match_all(). This is the syntax:

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

The first argument is the regex. The second is the subject. The third is the array, which holds all the matches. It is a two-dimensional array, here (For the preg_match() function it is a one dimensional array). The fourth argument is optional. We shall talk only about one flag for this argument.

Consider the following subject string:

$subject = "A cat is an animal. A rat is an animal. A bat is a creature.";

In the above subject, you have the sub strings: cat, rat and bat. You have cat first, then rat and then bat. Each of these sub strings match the following regex:

/[cbr]at/

This pattern will match only the first sub string, "cat".

Below are the top articles rated and ranked by Helium members on:

Regex modifiers in PHP

Helium Debate

Cast your vote!

Which is the better server operating system: Windows or Linux?

Click for your side.

87032

Featured Partner

Goldwater Institute

The Goldwater Institute was founded in 1988 by a small group of entrepreneurial Arizonans with the blessing of Senator Barry Goldwater. In keeping with the principles advanced by Senator Goldwater, the Goldwater Institute is dedicated to...more


CONNECT WITH US

Read
our blog
Helum for writers

Write and get published
Share with other writers
Polish your freelancing skills

Join our active writing community
Helium Content Source for Publishers

Quality articles from proven freelancers
Exclusive rights, fast turnaround
Brand engagement, business blogging -- our writers do it all

Get custom content today!

INFORMATION


Helium, Inc.
200 Brickstone Square Andover, MA 01810 USA
#