By using this site you agree to the use of cookies and related technologies.

Learn more

How to replace parts of a string with str_replace and preg_replace

Tutorial on how to replace parts of a string with str_replace and regular expressions.

Edited: 2016-04-09 09:29

Replacing substrings can be done using either regular expressions, and the native string functions. The most powerful method involves the use of regular expressions, while simple replacements can be done with either str_replace (case sensitive) or stri_replace (case insensitive).

The native str_replace function of PHP is used to replace all the occurrences of a given string with a replacement string. Using a regular expression will allow for more complex pattern-replacements, useful for HTML pages and the like.

Replacing string with str_replace

The str_replace function can be used by feeding it with a target string, a replacement string, and a source string. It is possible to use bare strings, variables and arrays. The first example uses bare strings:

$source_str = "...target1...target2...target3...target4";
echo str_replace("target2", "replacement2", $source_str);

It is also possible to use arrays, containing different strings to be replaced. The replacement will be performed in the order of the array:

$source_str     = "...target1...target2...target3...target4";

$targets        = array("target1", "target2", "target3");
$replacements   = array("replacement1", "replacement2", "replacement3");

$new_str = str_replace($targets, $replacements, $source_str);
echo $new_str;

Note. To perform a case insensitive match, the stri_replace function may be used.

Using regular expressions

Regular expressions may be used for more complicated replacements, such as when replacing HTML elements. In PHP we may use the preg_replace function to perform replacements with regex. Some may find it more complicated than performing replacements with str_replace, and it is in the sense that you need to learn how to write regular expressions, the effort is however worth it, as you can use it in many different cases.

Below is simple beginner regex that will replace multiple whitespace characters with just a single space.

preg_replace('/\s\s+/', ' ', $str);

In this case, the "s" is a short "code" for different whitespace characters, and the "+" signs can be translated to "one or more". The expression means something along the lines of: Where at least one whitespace character exists followed by one or more, replace this "matched pattern" with a single whitespace".

Sometimes you may want to "remember" certain parts of your source string while only replacing other parts. For example, you can remember the content of a HTML element, and only replace the element tags. In the below example we want to replace the paragraph tags with div tags, to do this we can "remember" the content, and replace the <p> tags around it. I.e.:

$str = '<!DOCTYPE html>
<html>

  <head>
    <title>My first Website</title>
  </head>

  <body>
    <p>My first Website.</p>
  </body>

</html>';

$new_str = preg_replace('#<p>([^<]*)</p>#', "<div>$1</div>", $str);

echo $new_str;

The regex used to match the pattern in the $str variable above is relativly simple:

#<p>([^<]+)</p>#

The part located inside the parentheses matches the content, basically it matches all characters except for the less than (<) sign. Hence the "[^<]" part. We already explained the plus sign ealier.

The Square brackets ([]) are used to match a series of unordered characters, in this case we used the caret/circumflex/hat sign to reverse the expression, and instead state which characters should not be matched.

The parentheses are used to remember the match as a "back reference", which allows us to insert it into the replacement string. Back references can be accessed by the numeric variables: $1, $2, $3. Etc. For nested parentheses, the matches are stored in the order that they are matched, from the inside-out – like the layers of an onion. This can be shown by a visual representation:

($3($2($1)))

So, whenever you work with expressions and nested parentheses, keep this in mind.

It is often a good idea to use reverse-logic in your expressions. For instance, instead of listing all the characters that you allow, it is often much easier to simply list those that you do not allow using the caret inside square brackets.

Finally, preg_replace also works with arrays in the same way that str_replace does.

$source_str     = "...<b>target1</b>...<i>target2</i>...<u>target3</u>...";

$targets        = array('#<b>([^<]*)</b>#', '#<i>([^<]*)</i>#', '#<u>([^<]*)</u>#');
$replacements   = array("<strong>$1</strong>", "<em>$1</em>", "<span>$1</span>");

$new_str = preg_replace($targets, $replacements_arr, $source_str);
echo $new_str;