Search Helium

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

Using regular expressions in PHP

by Chrys

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

Using Regular Expressions in PHP PHP Regular Expressions Part VII

Introduction We have seen some uses of regex in PHP. We know how to verify if a regex is found in a subject string. We know how to find the position of matched regex in the subject string. Note that the subject string can be a whole page of text. In this part of the series, we shall learn two important features titled "Search and Replace" and "The Split Operation". Before we leave this part we shall talk about the regex delimiter. Let us start with a very small section called Variables in Regex.

Variable in Regex Before we look at the two features, let us be aware that the regex pattern can have variables. The following code works:

$var = "am";

<?php $var = "am";

if (preg_match("/I $var/", "I am the one.")) echo "Matched" . "<br />"; else echo "Not Matched" . "<br />"; ?>

Here, we have the variable,

$var = "am";

The regex is

/I $var/

which is

/I am/

"am" in the pattern is replaced by $var; and matching occurs

Search and Replace You can search for a match in the subject string and have the sub strings matched replaced. Consider the following subject string:

"I am a man. You are a man."

The sub string "man" occurs in this subject in two places. You can have every occurrence of the sub string "man" replaced to woman. The following code does this:

<?php $finalStr = preg_replace("/man/", "woman", "I am a man. You are a man."); echo $finalStr; ?>

You have the function, preg_replace(). The first argument of this function is the regex. The second argument is the sub string to replace the sub string matched by the regex. The third argument is the subject.

This function returns the subject string in which all the matched sub strings have been replaced. If no match occurred, the original string is returned. If a mistake occurred, NULL is returned.

The second statement of the above code echoes the returned string. This is what is echoed:

I am a woman. You are a woman.

So, you can use the preg_replace() function to search for sub strings by a regex and have all the sub strings found replaced by some replacement sub string. The replacement sub string is the second argument of the function.

If there are many matches, it is possible for you to replace some of them, beginning from the first. For this, you need a fourth optional argument. Consider the following subject:

"I am a man. You are a man. That person is a man."

There are three occurrences of the word, "man". Let us have only the first two replaced. This

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

Using regular expressions in PHP

Helium Debate

Cast your vote!

Is Windows 7 Microsoft's best operating system?

Click for your side.

90554

Featured Partner

The Sunlight Foundation

Founded in January 2006, the mission of the Sunlight Foundation is to strengthen the relationship between lawmakers and their constituents by maximizing transparency of the work of Congress, its members, staff and lobbyists. Sunlight bel...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
#