I’m trying to escape a single quote within my PHP by adding a slash before it. Unfortunately, I’ve been unable to get it working with str_replace and I’m wondering if I’m doing something wrong.
What I have is the following …
$string = 'I love Bob's Pizza!';
$string = str_replace("'", "\\'", $string);
echo $string;
When I use this, for some reason it’s not replacing the single quote with “\'” as it should.
Any help is greatly appreciated!
Why not use addslashes?
UPDATE: If you insist on your way it’s because you’re not escaping the single quote. Try:
You simply can’t do what you’re doing because it causes a syntax error.