Andrej
Andrej

Reputation: 736

php json_encode for php < 5.4 (unexpected token)

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...

Upvotes: 0

Views: 2616

Answers (1)

Ryan
Ryan

Reputation: 1797

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.

Upvotes: 1

Related Questions