PHP str_replace Function
Another key tool to have in your programming toolbox is the
ability to quickly replace parts of a PHP string with new
values. The str_replace function is similar to a word
processor's "Replace All" command that lets you specify a word
and what to replace it with, then replaces every occurrence of
that word in the document.
str_replace Parameters
str_replace has three parameters that are required for the
function to work properly.
str_replace(search, replace,
originalString)
.
-
search - This is what you want to search your
string for. This can be a string or an array.
-
replace - All matches for search will be
replaced with this value. This can be a string or an array.
-
originalString - This is what search and replace
will be operating on. The str_replace function will
return a modified version of originalString when it
completes.
str_replace Simple Example
aImagine we are working at a school district and need to
create a webpage for the students' parents. The webpage has an
introduction string that we need to customize depending on if
the student is male or female. With str_replace this is
mighty easy.
PHP Code:
//string that needs to be customized
$rawstring = "Welcome Birmingham parents. Your replaceme is a pleasure to have!";
//male string
$malestr = str_replace("replaceme", "son", $rawstring);
//female string
$femalestr = str_replace("replaceme", "daughter", $rawstring);
echo "Son: ". $malestr . "<br />";
echo "Daughter: ". $femalestr;
Display:
Son: Welcome Birmingham parents. Your son is a pleasure to
have!
Daughter: Welcome Birmingham parents. Your daughter is a
pleasure to have!
With these two gender customized strings created we could
then provide a more engaging experience for the student's
parents when they logged into the school website with their
kid's credentials.
str_replace Arrays: Multiple Replaces in One
In the last example we only needed to replace one word
replaceme
in our string, but what if we wanted to replace
many words? We could just use the function multiple times to get
the job done, or we could create an array of
placeholders
and a second array of replace values to
get it all done in one function call.
The key thing to understand with this technique is that you
are creating two arrays that will be used to swap values. The
first item in placeholders will be replaced by the first
item in the replace values, the second item of
placeholders
replaced with the second in replace values
and so on and so forth.
Let's extend our simple example to be a complete form letter
addressed to a student's parents.
PHP Code:
//string that needs to be customized
$rawstring = "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!\"<br />";
//placeholders array
$placeholders = array('offspring', 'pronoun', 'pronoun2', 'sex');
//male replace values array
$malevals = array('son', 'he', 'him', 'boy');
//female replace values array
$femalevals = array('daughter', 'she', 'her', 'girl');
//male string
$malestr = str_replace($placeholders, $malevals, $rawstring);
//female string
$femalestr = str_replace($placeholders, $femalevals, $rawstring);
echo "Son: ". $malestr . "<br />";
echo "Daughter: ". $femalestr;
Display:
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!"
Notice: there is a bug in this code. The placeholder
pronoun2 did not get replaced in the way we intended (our
strings have he2 and she2 instead of him and her). This is
because all instances of pronoun were replaced first and
the pronoun in pronoun2 was replaced at this time with he or
she, making he2 or she2. When it was pronoun2's turn to
be replaced, there were no matches to be found, so our string
has no him or her.
To fix this bug you could simply make sure that pronoun2
comes first in the placeholders array and by updating the
values of the male and female replace values to reflect this.
PHP Code:
// ...snip
//placeholders array
$placeholders = array('offspring', 'pronoun2', 'pronoun', 'sex');
//male replace values array
$malevals = array('son', 'him', 'he', 'boy');
//female replace values array
$femalevals = array('daughter', 'her', 'she', 'girl');
//snip...
Display:
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!"
|