Hello Ninja, welcome to the new article on the PHP str_replace function. Have you ever created any website? You might have wondered if sometimes there is a string which is getting replaced by some other words in the website. For example, before payment, it shows your payment is not done and after your payment is done. So to implement this kind of feature, we need the PHP str_replace function.
This article will cover usage syntax and code example output of the PHP str_replace function.
PHP str_replace
There are many built-in functions; similarly, str_replace is also a built-in function of PHP. This function helps us to find a particular string and let us replace that string with sone another string. Below is the syntax of the str_replace function.
Below is an example of the PHP str_replace function.
Example
Suppose we have an org string and form the org string; we want to change a few words.
org=" Hello Ninja, welcome to the article of the PHP str_replace function".
And from the above string, we want to replace "welcome to" to "check out". So basically:
replace_from= "welcome to"
replace_to= "check out."
So below is the code to execute what we want:
Code Implementation
<?php
// Initial string we have.
$org=" Hello Ninja, welcome to the article of PHP str_replace function.";
$replace_from= "welcome to";
$replace_to= "check out";
// PHP str_replace function
$new_string = str_replace($replace_from, $replace_to, $org);
// Printing function in PHP.
echo($new_string);
?>
Output
Multiple String Replacement
Suppose you have a very large string, and for sub-particular strings you want to replace it with some other string. So this can also be implemented by the same PHP function.
For example:
Your original string is
org="Hello Ninja, Welcome to the new article on PHP str_replace function. Every coder must read this.”
And you want to replace Ninja with Champ
replace_from="Ninja" and "coder"
replace_to="Champ"
Below is the implementation of this.
Code Implementation
<?php
// Initial string we have.
$org="Hello Ninja, Welcome to the new article on PHP str_replace function. Every coder must read this.”;
// And you want to replace Ninja with Champ
$replace_from=array("Ninja","coder");
$replace_to="Champ";
// PHP str_replace function
$new_string = str_replace($replace_from, $replace_to, $org);
// Printing function in PHP.
echo($new_string);
?>
Output
Replacing Case-Insensitively
The PHP str_replace function is case sensitive, but what if we want to replace any kind of case word with the required one so this can also implement using the str_ireplace function. This function does not consider the case in the words. Check the below example:
Below is the implementation.
Code Implementation
<?php
// Initial string we have.
$org="Hello Ninja, Welcome to the new article on PHP str_replace function.”;
// And you want to replace Ninja with Champ
$replace_from=array("ninja");
$replace_to="Champ";
// PHP str_replace function
$new_string = str_ireplace($replace_from, $replace_to, $org);
//Printing function in PHP.
echo($new_string);
?>
Output
Replace Function
Below is the table of replace functions accepted in various programming languages.
Language
Function
Syntax
PHP
str_replace
str_replace(changeFrom, changeTo, originalString)
Python
replace
str.replace(old, new[, count])
JavaScript
replace
str.replace(changeFrom, changeTo)
Ruby
gsub
str.gsub(pattern, replacement)
Java
replace
str.replace(changeFrom, changeTo)
C#
Replace
str.Replace(changeFrom, changeTo)
Now we discuss some FAQ-related PHP str_replace functions.
Is it possible to replace multiple spaces with single spaces using php str_replace?
Yes, it is very easy; we just need to “find” the double space string, and in replace, we just need to assign the single space string. The programme will work accordingly.
Can the special characters be replaced in the php str_replace function?
Yes, we can do it simply following the same syntax where in “find” the special character we should assign and in replace what we want to replace. It will work perfectly.
Does every language have this replacement function?
In most languages, the replacement function is available. In fact, we can create a similar function with our logic too. It is very simple. Comment below to know how we can do it.
Can I replace multiple substrings with str_replace?
Yes, you can replace multiple substrings by providing arrays of old and new substrings as parameters.
What's the difference between str_replace and str_ireplace?
str_replace is case-sensitive; if there is any difference in the case of string, the function will not work properly, while str_ireplace is case-insensitive; here, the case does not matter.
Conclusion
So finally, you have completed this article. I hope you have learned a lot from this article, in which we have covered topics like PHP str_replace, Multiple String Replacement, Replacing Case-Insensitively, and their implementation and output. If you want to learn more about this related topic, check out the links below.