String (JSON): {"title":"\"Copper\""}
I have tried
$output = str_replace('\\"', "", $output);
but I got
{"title":"" Copper""}
What I expect
{"title":"Copper"}
String (JSON): {"title":"\"Copper\""}
I have tried
$output = str_replace('\\"', "", $output);
but I got
{"title":"" Copper""}
What I expect
{"title":"Copper"}
Your example code is incomplete, because what you have given does work:
php > $s='{"title":"\"Copper\""}';
php > echo $s;
{"title":"\"Copper\""}
php > echo str_replace('\\"',"",$s);
{"title":"Copper"}
(that is from a php -a
interactive session)
In other words, your input data is not what you think it is.
BTW, as others have said, it would be better to use json_decode
on the JSON. Then do string manipulations on just the field of interest. Then use json_encode
to turn it back in JSON. Using str_replace
or regexes may work for simple cases, but as soon as you get unexpected data it can go horribly wrong.
Are you generating the JSON string with your own code (as opposed to getting it from a third-party)? If so, post the code which generates the JSON string and detail what you have done to this point to try and resolve the problem.
If you don't have control over the JSON string (and/or the creator of the JSON string will not/cannot fix it), then a hacky way to fix it would be:
$output = str_replace( '\"' , '' , $output );