str_replace() replaces all occurrences of search string with a replacement string.
Syntax
str_replace(find, replace, string, count)
- find(required) – It specifies the value to find.
- replace(required) – It specifies the value to replace the value in find.
- string(required) – it specifies the string to be searched.
- count(optional) – it is the variable that counts the number of replacements.
For example
echo str_replace("world","John","Hello world!",$i);
echo "Replacements #: $i";
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
- If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as replace
- If find is an array and replace is a string, the replace string will be used for every find value
Did this post help you?
Tutsplanet brings in-depth and easy tutorials to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply