Simple PHP string replace?

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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Have a look at str_replace

$abc = ' Hello "Guys" , Goodmorning';
$abc = str_replace('"', '$^', $abc);

Solution 2


Should work for you.

Solution 3

$abc = ' Hello "Guys" , Goodmorning';

$new_string = str_replace("\"", '$^', $abc);
echo $new_string;


Hello $^Guys$^ , Goodmorning

Solution 4

preg_replace('/"/', '$^', $abc);

Solution 5

Searching the manual would have brought you to this:

str_replace('"', '$^', $abc);

Solution 6

You can use str_replace:

$abc = ' Hello "Guys" , Goodmorning';
echo str_replace('"','$^',$abc);

Solution 7

String replace function is used to replace string. Your syntax is wrong. You have to use php string function like below example. First of all, think about first and two values and replace in third. Let’s have a look.

echo str_replace("Hello", "HI", "Hello Jack "); 

It produces the output.

HI Jack.

You can create an HTML form and change on button only.

input box 1 
input box 2 
input box 3 




Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply