Broad Network


Using Regular Expressions in PHP

PHP Regular Expressions – Part VII

Forward: In this part of the series, we shall learn two important features titled “Search and Replace” and “The Split Operation”.

By: Chrysanthus Date Published: 11 Aug 2012

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 following code does this:

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

A fourth argument has been added. We want the first two occurrences of the sub string, “man” to be replaced, so we put 2 as the fourth argument. The fourth argument is the number of occurrences of the match, you want replaced. The second statement in the code above echoes the resulting string as,

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

This fourth argument is optional. If you put –1 there it means you want all occurrences of the matched sub string to be replaced; this is the default behavior in the absence of the –1. The following example illustrates the use of the –1.

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

The output of the code is:

         I am a woman. You are a woman. That person is a woman.

So far we have seen the preg_match() function, the preg_match_all() function and now the preg_replace() function.

Counting the number of Replacement Made
There is a fifth optional argument for the preg_replace() function. This is a variable. You choose whatever name you want for the variable. The following code illustrates this:

<?php
    $finalStr = preg_replace("/man/", "woman", "I am a man. You are a man. That person is a man.", 2, $count);
    echo $finalStr."<br />";
    echo $count."<br />";
?>

Look at the arguments of the function very well. The first argument is the regex. The second is the replacement string. The third is the subject; it has three occurrences of the word, “man”. The fourth gives the number of replacement we want; it is 2. The fifth is the variable that will hold the number of replacements actually made. The output of the code is,

I am a woman. You are a woman. That person is a man.
2

The fourth argument wanted two replacements and two replacements were made. The last statement in the code displays the number of replacement actually made; it displays the value of the variable, which is the fifth argument. It is not necessary for you to have declared this variable elsewhere, before.

It is possible that you would not know the number of matches in the subject, and you would want to replace all possible matches and know the number of replacement. In this case the fourth argument will be –1 and you would need the fifth argument. The following code illustrates this:

<?php
    $finalStr = preg_replace("/man/", "woman", "I am a man. You are a man. That person is a man.", -1, $count);
    echo $finalStr."<br />";
    echo $count."<br />";
?>

Here all the matches would be replaced and $count would hold the number of replacement, after execution of the function. The output of the above code is,

I am a woman. You are a woman. That person is a woman.
3

Three is the number of replacement made.

The Split Operation
There is a function for this. A syntax for the function is:
array preg_split(string $regex , string $subject)

The split function preg_split() splits a string into an array of sub strings and returns the array. It uses a regex as a separator. Be default the regex is not any element of the returned array. Consider the following subject string:

$subject = "one two three";

If we know the regex pattern to identify space between words, then we can split this string into an array made up of the words, “one”, “two” and “three”.   is the character class for space. + will match a space, one or more times. The regex to separate the above words is

               \ +
We assume that a space might be created by hitting the spacebar more than once. The following code illustrates the use of the split function with the above pattern.

<?php
    $subject = "one two three";
    $arr = preg_split("/ +/", $subject);
    echo $arr[0]."<br />";
    echo $arr[1]."<br />";
    echo $arr[2]."<br />";
?>

In the subject string the words are separated by spaces. The output of the above code is:

one
two
three

The spilt function has split the words in the subject string using the space between the words, and put each word as an element of the returned array.

It is possible to have words in a string separated by a comma and a space, like

$subject = "one, two, three";

The regex to separate these words is:

          /, +/

The following code illustrates this:

<?php
    $subject = "one, two, three";
    $arr = preg_split("/, +/", $subject);
    echo $arr[0]."<br />";
    echo $arr[1]."<br />";
    echo $arr[2]."<br />";
?>
The output of the above code is:

one
two
three

A simplified syntax of the preg_replace() is:

$StrWithRepl = preg_replace(string regex, string $replacement, string $subject);

From the explanation I have given before, the syntax here is self-explanatory.

The Delimiters
Must you always use the // delimiters for the regex? No. PHP gives you the possibility of using delimiters of your choice.

The following expressions, each produces a match:

    preg_match("!World!", "Hello World")
    preg_match("{World}", "Hello World")
    preg_match("'World'", "Hello World")

The // default delimiters for a match can be changed to arbitrary delimiters. In the first example, the delimiters are !!. In the second expression the delimiters are {}. In the third example, the delimiters are ''.

The following code illustrates the third case:

<?php
    if (preg_match("'World'", "Hello World"))
     echo "Matched" . "<br />";
    else
     echo "Not Matched" . "<br />";
?>

Wow, we have done a lot. We have just one more part of the series to see. All what we have done so far is good. You can do a lot with what we have done. I showed you in the previous part of the series, how to solve problems that are involving. In the next part of the series, we shall cover features, which you will want when you need more power in regex. These features are not always used, but you would need them occasionally. The next and last part of the series is titled, More Regular Expressions in PHP.

So, let us take a break here and continue in the next part.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message