In this tutorial, we learn PHP str_replace Function with help of multiple types of examples. How we can use the PHP str_replace function at the time of development.
Here are some important points which we will discuss in this tutorial,
- Example of PHP string replace.
- How to use PHP str_replace with arrays.
- PHP str_replace to replace multiple strings or values.
- Replace using PHP str_replace case insensitive.

In This Article
- 1 Introduction to PHP string replace Function
- 2 Syntax
- 3 Parameter Values
- 4 Example of PHP str_replace Function
- 5 PHP str_replace Function With an Arrays Code Example
- 6 PHP str_replace to Replace Multiple Values or String Code Example
- 7 Replace String Using PHP str_ireplace With Case Insensitive
- 8 Conclusion
Introduction to PHP string replace Function
PHP string replace function is used to replace specified characters with other characters from the string.
The function works followed by these rules,
- PHP string replace function check character case (case sensitive).
- If the string searched in an array then it also returns an array.
- In array serach and replace, PHP replace function check all the array element.
Syntax
1 |
str_replace(find,replace,string,count) |
Return Value: Returns a string or an array with the replaced values
Parameter Values
Parameter | Description |
---|---|
find | Specifies the value to find (Required) |
replace | Specifies the value to replace the value in find (Required) |
string | Specifies the string to be searched (Required) |
count | A variable that counts the number of replacements (Optional) |
Example of PHP str_replace Function
1 2 3 4 5 |
<?php $str = "Example of PHP string replace function."; $modifyString = str_replace("Example","Best example", $str); echo $modifyString; ?> |
Output: Best example of PHP string replace function.
PHP str_replace Function With an Arrays Code Example
1 2 3 4 5 6 7 |
<?php $find = array("Hello","world"); $replace = array("B"); $arr = array("Hello","world","!"); print_r(str_replace($find,$replace,$arr)); ?> |
Output:

PHP str_replace to Replace Multiple Values or String Code Example
1 2 3 4 5 6 7 8 9 |
<?php // Provides: You should eat pizza, beer, and ice cream every day $phrase = "You should eat fruits, vegetables, and fiber every day."; $healthy = ["fruits", "vegetables", "fiber"]; $yummy = ["pizza", "beer", "ice cream"]; $newPhrase = str_replace($healthy, $yummy, $phrase); echo $newPhrase; ?> |
Code Highlights:
In this example, there is a string statement where we do apply the find and replace function to replace multiple values or strings.
Now we create two arrays where we place it into the str_replace()
function and then replace one array element with another array element.
Output:

Replace String Using PHP str_ireplace With Case Insensitive
To find and replace without checking character cases we can use PHP str_ireplace function.
1 2 3 4 5 6 7 |
<?php $arr = array("blue","red","green","yellow"); // This function is case-insensitive $newArray = str_ireplace("RED","pink",$arr,$i); print_r($newArray); ?> |
Output:

Conclusion
Here we learn the complete PHP string replace function’s functionality and implementation.
And we also learn PHP str_ireplace function for case insensitivity with example.
To run and check by using our online code editor Run PHP Code (phpcoder.tech).
you check more PHP string functions here PHP: String Functions – Manual.
I hope you all understand the complete concept of PHP string replacement with an array and also replace multiple values replacement.
Happy Coding..!
One thought on “PHP str_replace Function | Complete Guide”