PHP String Replace

August 26, 2013

We have already seen about many of PHP string functions with this blog, like implode/explode, string extract, split and etc. But, it is never ending process of learning about PHP Strings and Arrays. Since, in that much volume of PHP core functions are there in this area. Not only in volume, these functions have gained that much popularity also.

Again, we are going to deal with one more PHP string functionality replacing required string in a given context. str_replace() is the most widely used function in PHP for string replace. This will search for given string in the context given as well, and replace it with another string which is also provided with this function for replacement.

php_string_replace

PHP str_replace()

This function accepts four the following list of parameters.

  1. $string_search – This parameter contains string value to be searched.
  2. $string_replace – It contains another string used for replacement of $string_search.
  3. $context – This is the one subject to what, the string search and replace will take place.
  4. $count – This is an optional parameter. And, this will be set with the count about to the number of replacement made with str_replace() invoke.

Note:

  • The forth parameter specified as $count in the above list should be as a variable reference. Anything other than that will cause the following error.
    Fatal error: Only variables can be passed by reference in ... on line ...
    
  • First three mandatory parameters of PHP str_replace will accept data mixed data type, for example, it can also be an array, instead of string.

Simple Example for PHP str_replace

First, let us have a simple example for this PHP function by only dealing with string data without any mixed type instead. For that, the following PHP program invoke this function by sending all of its parameters, including the optional count parameter to store the number of replacement made with str_replace().

And, we can see that all such parameters are having string data except forth one specified as variable reference.

<?php
$orginal_string = "Welcome to PHP Blog";
echo "<p><b>Original Context: </b>" . $orginal_string . "</p>";
$replace_string =  str_replace(" ","<br/>",$orginal_string,$count);
echo "<p><b>Context After String Replace:</b><br/>" . $replace_string. "</p>";
echo "<p><b>Number of Replacement: </b>" . $count. "</p>";
?>

In the above program, we are searching for white spaces of the given string context to replace them with line breaks. Since, we have added HTML <BR> tags, this breaks will be reflected in the browser output, as like as,

Original Context: Welcome to PHP Blog
Context After String Replace:
Welcome
to
PHP
Blog
Number of Replacement: 3

In the original context, we can found three white spaces to be replaced with the given replacement string. And, this count is added to the $count variable specified as forth parameter of str_replace(), and displayed to the browser by using the echo statement.

str_replace with PHP Arrays

Now, we are going to see how str_replace will work with PHP arrays. The following PHP program contains a master array instead of string context specified for the above example, subject to what the search string will be replaced.

<?php
$orginal_array = array("Welcome to the Blog", "the string functions");
echo "<PRE>";
echo "<b>Original Array: </b>";
print_r($orginal_array);
$replaced_array =  str_replace("the","PHP",$orginal_array,$count);
echo "<p><b>Array after String Replace with Each Entry:</b>";
print_r($replaced_array);
echo "<p><b>Number of Replacement: </b>" . $count. "</p>";
?>

There are two elements in the master array, and each of these element will be search for the string the and replaced with PHP. Since, there are two occurrences of the search_string, count will be set with this number. And then, the output of the program will be as follows.

Original Array: 
Array
(
    [0] => Welcome to the Blog
    [1] => the string functions
)
Array after String Replace with Each Entry:
Array
(
    [0] => Welcome to PHP Blog
    [1] => PHP string functions
)
Number of Replacement: 2

str_replace with Arrays for All Parameters

Similarly, we can apply array data for all of the str_replace parameters. let us have an another PHP example for that.

$orginal_array = array("Welcome to the Blog", "the string functions");
$search_array = array(" ", "the");
$replace_array = array("<br/>", "PHP");
echo "<PRE>";
echo "<b>Original Array: </b><br/>";
print_r($orginal_array);
$replaced_new_array =  str_replace($search_array,$replace_array,$orginal_array,$count);
echo "<p><b>Array after String Replace with Each Entry:</b><br/>";
print_r($replaced_new_array);
echo "<p><b>Number of Replacement: </b>" . $count. "</p>";

Each of the search array element is replaced with the replacement array element. And, the output is shown below withthe number of replacement taken place.

Original Array: 
Array
(
    [0] => Welcome to the Blog
    [1] => the string functions
)
Array after String Replace with Each Entry:
Array
(
    [0] => Welcome
to
PHP
Blog
    [1] => PHP
string
functions
)
Number of Replacement: 7

Note:

While using array data, replacement behavior will be changed with the mismatch of the array length, if any.

If the search array length is less than replacement array length, then the replacement will be skipped for that particular replacement array entry which doesn’t have corresponding search entry. For example,

$search_array = array(" ");
$replace_array = array("
", "PHP");

Since, there is only one element in search array, str_replace will search master array for this element and replaced with the first entry of replacement array. And will skip the replacement with second entry of $replace_array, PHP.

On the other hand, if $replace_array length is less than $search_array, then the search entry which doesn’t have corresponding replacement entry will be replaced with an empty string. For example,

$search_array = array(" ","the");
$replace_array = array("<br/>");

In this example white spaces are replaced with HTML line breaks and string the is replaced with an empty string, since there is no entry specified for $replace_array.

Download PHP String Replace Source Code

Add a Comment