I have this code:
$abc = ' Hello "Guys" , Goodmorning';
I want to replace every occurrence of "
(double quotes) by $^
so that string becomes
'Hello $^Guys$^ , Goodmorning'
I am new to PHP; in Java we can do this very easily by calling the string class replaceAll
function, but how do I do it in PHP? I can't find the easy way on Google without using regular expressions.
What is some syntax with or without the use of regular expressions?
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.
Python String replace() Method The replace() method replaces a specified phrase with another specified phrase. Note: All occurrences of the specified phrase will be replaced, if nothing else is specified.
The str_replace() is a built-in function in PHP and is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively.
Have a look at str_replace
$abc = ' Hello "Guys" , Goodmorning';
$abc = str_replace('"', '$^', $abc);
str_replace('"','$^',$abc);
Should work for you.
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