0

I have a jquery ajax which invokes a php file which outputs a json object.

It all works great on versions of php higher than 5.3, but lower versions don't have JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, and JSON_UNESCAPED_UNICODE implemented yet.

How do I escape "json sensitive" chars like "&" or "=" in earlier versions?

Thank you for your time!

EDIT: scratch that.. the problem is => i have <br /> in the text and that gets changed to <br \=""> the " gives the error...

29
  • 1
    I'm pretty sure & and = are treated as normal characters in JSON, you're probably thinking of URLs. Commented Jun 7, 2012 at 19:30
  • no, i get the "SyntaxError: Unexpected token =" error when parsing.. same goes for "&" Commented Jun 7, 2012 at 19:33
  • 3
    @Andrej Example, please. Commented Jun 7, 2012 at 19:34
  • 1
    @Andrej: That's not valid JSON. Commented Jun 7, 2012 at 19:41
  • 1
    @Andrej json_decode(json_encode('<br />')) === '<br />' — So what exactly are you doing? Commented Jun 7, 2012 at 20:06

1 Answer 1

1

If you're using UTF-8 Encoding, you can use this:

$json = preg_replace('/[^(\x20-\x7F)]*/','', $json);  

For JSON_UNESCAPED_SLAHES, you can use:

preg_replace('\\/', '/', $json);

JSON_UNESCAPED_UNICODE might be a bit more complicated, I found some examples on php.net manual here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.