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 that we will discuss in this tutorial,
- Example of PHP string replace function.
- How to use PHP str_replace with arrays.
- PHP str_replace to replace multiple strings or values.
- Replace using PHP str_replace case insensitive.
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 is searched in an array then it also returns an array.
- In array search and replace, PHP replace function checks all the array elements.
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 that we place 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 (codingtasks.net).
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..!
[…] PHP str_replace Function | Complete Guide you can learn complete guide with different […]
[…] PHP str_replace Function | Complete Guide […]