I want to be able to replace spaces with -
but also want to remove commas and question marks. How can I do this in one function?
So far, I have it replacing spaces:
str_replace(" ","-",$title)
Approach 1: Using the str_replace() and str_split() functions in PHP. The str_replace() function is used to replace multiple characters in a string and it takes in three parameters. The first parameter is the array of characters to replace.
The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.
Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.
You can pass arrays as parameters to str_replace()
. Check the manual.
// 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With