Tutorial Ref.com

PHP preg_replace Example

Tutorial Ref >> PHP Tutorials >> PHP Preg_Replace Example:

Date modified - May 13, 2009

The following is a simple example of the PHP function preg_replace(). It's also a short tutorial on regex (regular expressions) using Perl since this is the regex patterns the preg_replace function utilizes.

Here is some simple text that we wish to parse:

1
2
3
4
{subtitle}Subtitle of first paragraph{/subtitle}
<p>The first paragraph</p>
{subtitle}Subtitle of second paragraph{/subtitle}
<p>The second paragraph</p>

So now we place the paragraph in a PHP variable like so:

1
2
3
4
5
6
<?php
$string = {subtitle}Subtitle of first paragraph{/subtitle}
<p>The first paragraph</p>
{subtitle}Subtitle of second paragraph{/subtitle}
<p>The second paragraph</p>
?>

As for the preg_replace function here is the code:

1
2
3
4
<?php
$regex = "#([{]subtitle[}])(.*)([{]/subtitle[}])#e";
$output = preg_replace($regex,"('<strong>$2</strong>')",$string);
?>

So the above basically searches for all text between the special 'subtitle' tags and includes the subtitle tags themselves. Then it parses the second parathesized pattern and this is placed between the <strong> tags.

PHP preg_replace Regex

So let's go step-by-step through the regex now. The hash character (i.e. '#') at the beginning and end is the indicator for the beginning and end of the regex pattern. I have seen coders use an at sign (i.e. '@') and this character is more commonly represented as a forward slash (i.e. '/'). The first paranthesized pattern is:

1
2
3
<?php
([{]subtitle[}])
?>

The first set of square brackets (i.e '[]') means to match for for the open curly bracket (i.e. '{'). Then the regex pattern searches for the text 'subtitle'. The last set of square brackets will match for the closing curly bracket (i.e. '}'). Alternatively, you can escape the curly brackets like so: '\{'.

The second paranthesized pattern is:

1
2
3
<?php
(.*)
?>

The period (i.e. '.') matches any character except a new line. The asterik (i.e. '*') matches the previous item zero or more times. For example, if we had:

1
2
3
<?php
(x*)
?>

The regex pattern will match zero 'x', one 'x', two 'xx's, and so forth.

The third paranthesized pattern is:

1
2
3
<?php
([{]/subtitle[}])
?>

The above is the same pattern as the first paranthesized pattern except the text portion '/subtitle' contains the forward slash which indicates this is the closing tag (i.e. '{/subtitle}').

The e modifier or flag is best described on the php.net site: "When using the e modifier, this function escapes some characters (namely ', ", \ and NULL) in the strings that replace the backreferences. This is done to ensure that no syntax errors arise from backreference usage with either single or double quotes (e.g. 'strlen(\'$1\')+strlen("$2")'). Make sure you are aware of PHP's string syntax to know exactly how the interpreted string will look like."

preg_replace example

Let's look at the preg_replace now:

1
2
3
<?php
$output = preg_replace($regex,"('<strong>$2</strong>')",$string);
?>

We've already looked at the first parameter '$regex' and its pattern and the third parameter '$string' is simply the text portion where the regex would be searching for matches. The second parameter is as so:

1
2
3
<?php
"('<strong>$2</strong>')"
?>

So the above is simply replacing this text in the $string:

1
{subtitle}Subtitle of first paragraph{/subtitle}

With this text:

1
<strong>Subtitle of first paragraph</strong>

The variable '$2' represents the match of 'Subtitle of first paragraph'.

The entire output would be:

1
2
3
4
<strong>Subtitle of first paragraph</strong>
<p>The first paragraph</p>
<strong>Subtitle of second paragraph</strong>
<p>The second paragraph</p>

If were to use '$1' and '$3' in the preg_replace function they would represent the first paranthesized parameter of the regex (i.e. '{subtitle}') and the second paranthesized parameter (i.e. '{/subtitle}'), respectively.

So if we used in the regex as so:

1
2
3
4
<?php
$regex = "#([{]subtitle[}])(.*)([{]/subtitle[}])#e";
$output = preg_replace($regex,"'$1'.'$2'.'$3'",$string);
?>

The output would be be the same as the variable $string:

1
2
3
4
{subtitle}Subtitle of first paragraph{/subtitle}
<p>The first paragraph</p>
{subtitle}Subtitle of second paragraph{/subtitle}
<p>The second paragraph</p>

In this article, a simple preg_replace example was explained. I also explained step-by-step the regex pattern for PHP's preg_replace function. The parameters for preg_replace were also discussed. I hope this helps someone begin to understand regex and preg_replace. Happy coding!

TutorialRef.com - tutorials, guides, how-tos, helps