Welcome back. We’re in the midst of the holidays and I hope ever one is having a good time. I know It has been eventful so far for me. Anywho, this week I’ll do a quick overview of the str_replace() function. This is another function I use often in my projects. It is useful when you need to quickly replace parts of a string. It doesn’t take regular expressions so if you don’t need them then this is the function for you as it is quicker and less processor intensive. Please note however that it is case-sensitive. If you need to replace with case-insensitive strings use str_ireplace(). This function acts identical to str_replace except that it is case-insensitive.
Moving forward. Here is the official description taken from php.net for the str_replace() function:
Replace all occurrences of the search string with the replacement string
The function takes three required parameters and a fourth optional parameter. They are as follows:
mixed $search mixed $replace mixed $subject int $count [optional]
The first parameter, $search, is the values you want to replace. This can be a single string or an array of multiple strings.
We next have $replace. This is the values you want to replace the $search values with. If $search is an array and $replace is a string then it will replace all occurrences in $search with the same value. If $replace is an array with a smaller size then $search then all remaining $search terms will be replaced with an empty string.
Lastly is $subject. This is the string or array that will be searched through. If it is an array each element will have the search and replace action performed. str_replace() will also return an array if the $subject is an array.
$count is the only optional variable. If a variable is passed it will be filled with the number of matches and replacements that have occurred.
Now that we have defined the function and gone over the parameters here are some examples:
// replace some values in a simple string $string = "The house on the left is made up of wooden boards"; $modified = str_replace("left","right",$string); echo $modified; // Outputs: The house on the right is made up of wooden boards $modified = str_replace(array("left","wooden"),"green",$string); echo $modified; // Outputs: The house on the green is made up of green boards $modified = str_replace(array("left","wooden","boards"),array("green","stone"),$string); echo $modified; // Outputs: The house on the green is made up of stone $modified = str_replace(array("left","wooden boards"),array("right","brick"),$string); echo $modified; // Outputs: The house on the right is made up of brick
The above examples are pretty simple but I think they show the basic use of the str_replace() function. Now there are a couple things to watch out for when using this function. You need to take care with the order you are replacing items with. The function will replace all items in the first index of the $search array, then continue to the next index and so on and so forth. This may give unexpected results. Here is an examples of this.
$string = "a simple apple tree"; $modified = str_replace(array("a","e"),$array("e","X"),$string); echo $modified; // Outputs: X simplX XpplX trXX
In this example it replaces all occurrences of “a” with “e“. It then replaces all occurrences of “e” (including the ones that were placed when “a” was replaced) with “X“. I don’t run into this that often but it is something you will want to keep in mind.
Well this is it for this week. I hope as always that is was helpful to someone out there 🙂 Next week I’ll deal with strstr(). As always, thanks for reading.
– Miah