PHP str_replace

str_replace()-Replace a string

The “str_replace()” function is used to replace some occurence in a string. It is very much similar to the “replace all” function in a word processor. It let’s us specify a word and what to replace it with, then replaces every occurrence of that word in the document. The syntax of the “str_replace()” function is:

str_replace(search, replace, string);

Let’s have a look at the above syntax. We will discuss each argument now.

  • search:This is the string that has to be replaced.
  • replace:This is the value with which the “search” string has to be replaced.
  • string:This is the string within which the whole replacement procedure has to be done.

We will see a simple example here in which we customise a string depending on whether a person is an adult or a child.

<?php $str="I am rep."; $str_ad=str_replace("rep", "adult", $str); $str_ch=str_replace("rep", "not an adult", $str); echo "adult:".$str_ad."<br/>; echo "not adult:".$str_ch; ?>

The output will be:

adult:I am adult. not adult:I am not adult.

In the above example, we had to replace a single value in the string. Suppose we want to replace multiple values in a single string. For this we will use arrays. We need two arrays for this purpose-one will store the “search” values and the other will store the “replace” values. Let’s see this with an example.

<?php $str="Welcome Birmingham parent! Your offspring is a pleasure to have! We believe pronoun is learning a lot. The faculty simple adores pronoun2 and you can often hear them say  "Attah sex!""; $search=array('offspring', 'pronoun', 'pronoun2', 'sex'); $malerep=array('son', 'he', 'him', 'boy'); $femalerep=array('daughter', 'she', 'her', 'girl'); $malestr=str_replace($search, $malerep, $str); $femalestr=str_replace($search, $femalerep, $str); echo "Son: ".$malestr."<br/>"; echo "Daughter: ".$femalestr; ?>

The output of the above script will be:

Son: Welcome Birmingham parent! Your son is a pleasure to have! We believe he is learning a lot. The faculty simple adores he2 and you can often hear them say "Attah boy!"  Daughter: Welcome Birmingham parent! Your daughter is a pleasure to have! We believe she is learning a lot. The faculty simple adores she2 and you can often hear them say "Attah girl! 

The above output is inconsistent with what we wanted. The “pronoun2” does not reflect the result that we wanted. Can you guess the reason?
The reason is very simple. The “str_replace” works step by step. First it will replace all the “offspring” value in the string, then it will replace all the “pronoun” values in the string and then it will proceed to “pronoun2” and then in the end to “sex” value. So when the function is replacing the “pronoun” value, it encounters “pronoun2” in the string and replaces it. Hence, the “pronoun” in “pronoun2” was replaced with “he” or “she”. So when the “str_replace” function started searching for “pronoun2”, it did not find any match in the string. Hence, the error.
A simple solution to this problem can be to place “pronoun2” before “pronoun” in the “search” array. So, the above code can be written correctly as:

<?php $str="Welcome Birmingham parent!<br/> Your offspring is a pleasure to have! We believe pronoun is learning a lot.<br/> The faculty simple adores pronoun2 and you can often hear them say  "Attah sex!""; $search=array('offspring', 'pronoun2', 'pronoun', 'sex'); $malerep=array('son', 'him', 'he', 'boy'); $femalerep=array('daughter', 'her', 'she', 'girl'); $malestr=str_replace($search, $malerep, $str); $femalestr=str_replace($search, $femalerep, $str); echo "Son: ".$malestr."<br/>"; echo "Daughter: ".$femalestr; ?>

And the output will be:

Son: Welcome Birmingham parent! Your son is a pleasure to have! We believe he is learning a lot. The faculty simple adores him and you can often hear them say "Attah boy!"  Daughter: Welcome Birmingham parent! Your daughter is a pleasure to have! We believe she is learning a lot. The faculty simple adores her and you can often hear them say "Attah girl!"

which is the result that we desired.

If you have found my website useful, please consider buying me a coffee below 😉