What is the simplest way to replace text in a string using PHP?

Solution: 1

You can use str_replace() to replace characters in a string:


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

echo $abc; 
// Output: Hello $^Guys$^, Goodmorning

✅ This approach directly replaces all occurrences of ” with your chosen string ($^).

Solution: 2

You can replace double quotes using str_replace() with an escaped quote:


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

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

// Output:
// Hello $^Guys$^, Goodmorning

✅ This method works the same as Solution 1 but explicitly escapes the double quote.