0

I should send Hi" to a Yahoo server, so in PHP I should place \ befor the ", but it will get bad JSON arguments. How should I do it?

Place Hi" in JSON code without error?

$message = "Hi\"";
$postdata = '{
             "message" : "'.$message.'"
             }';
Jonas
  • 109,920
  • 93
  • 295
  • 369
Moein Hosseini
  • 4,189
  • 15
  • 65
  • 101

2 Answers2

5

Use json_encode instead of hand-crafting JSON:

$postdata = json_encode(array("message" => $message));

If you must handcraft your JSON, don't forget to add a backslash before a quotation mark:

$message = "Hi\\\"";
// or, more clearly ...
$message = 'Hi\\"';
phihag
  • 263,143
  • 67
  • 432
  • 458
1

New line character will not work in case of Tooltip with some browsers.
Not working \r\n or \n
Not working single quotes \'abcd

Use double backslash to escape characters.

Solution : use '\\\r\\\n' in place of '\r\n' ,
it will solve your problem.
Happy coding...!

amesh
  • 1,261
  • 3
  • 20
  • 51