PHP str_replace() is an inbuilt string function that replaces some characters with some other characters in the string. The str_replace() is the built-in function in PHP and is used to replace all the occurrences of a search string or array of search strings by the replacement string or array of replacement strings in the given string or array respectively.
PHP String Replace Example
You can replace some part of the string with the provided string value. If the search or replace are arrays, their elements are processed first to last. The str_replace() function works by the following rules.
- If a string to be searched in the array, it returns an array.
- If a string to be searched in the array, find and replace is performed with every array element.
- If both find and replace are arrays, and replace has fewer elements than found, the empty string will be used as a replacement.
- If the finding value is an array, and replacement is the string, the replacement string will be used for every find value.
The str_replace() function is case-sensitive. Use the str_ireplace() function to perform a case-insensitive search.
Syntax
str_replace(find, replace, string, count)
The find parameter is required, and it specifies the value to find.
The replace parameter is required, and it specifies the value to replace the value in finding.
The string parameter is required, and it specifies the string to be searched.
The count parameter is optional, and it is the variable that counts the number of replacements.
See the following code example.
<?php // app.php $data = array("mysql","pgsql","mongodb","oracle"); print_r(str_replace("pgsql", "mssql", $data, $i)); echo "Replacements: $i";
Here, we have replaced only one item of the array that is why the count will be 1.
See the output.
PHP str_replace() replaces left to right; it might replace a previously inserted value when doing multiple replacements.
See another example.
<?php // app.php $xyz = 'Mark|Elon|Jeff|Bill|Larry'; $result = str_replace('|', '---', $xyz); print($result);
See the following example.
Faster Method
A faster way to replace the strings in multidimensional array is to json_encode() it, do the str_replace() and then json_decode() it, like the following code syntax.
<?php function str_replace_json($search, $replace, $subject){ return json_decode(str_replace($search, $replace, json_encode($subject))); }
This method is almost 3x faster (in 10000 runs.) than using recursive calling and looping method, and 10x more straightforward in coding.
Using str_replace() with fewer elements in replace than find
See the following code.
<?php // app.php $finding = array("app","dividend"); $replacement = array("xcode"); $arrA = array("app","!"); print_r(str_replace($finding, $replacement, $arrA));
See the output.
Be careful when replacing characters (or repeated patterns in the FROM and TO arrays). See the following code.
<?php // app.php $arrFrom = ["1","2","3","B"]; $arrTo = ["A","B","C","D"]; $word = "ZBB2"; echo str_replace($arrFrom, $arrTo, $word);
See the following output.
I would expect as a result: “ZDDB.” However, this return: “ZDDD.” Be aware that if you use this for filtering & sanitizing some form of user input, or remove ALL instances of a string.
Finally, PHP str_replace Example | PHP str_replace() Function Tutorial is over.