Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to decode JSON stripslashed String?

Tags:

json

php

Does anyone know why this happens?

var_dump(json_decode(stripslashes(json_encode(array("O'Reiley"))))); // array(1) { [0]=> string(8) "O'Reiley" } 
var_dump(json_decode(stripslashes(json_encode(array("O\'Reiley"))))); // NULL

Are ' used at all by the JSON functions?

like image 273
Alix Axel Avatar asked Dec 05 '25 10:12

Alix Axel


1 Answers

I don't know for sure, but json_last_error() should :)

My guess, though, is that json_encode() does something to the \' that the stripslashes() then breaks - e.g. add another "\" to escape the backslash.

Isn't fiddling with a json encoded string using striplslashes() before it's decoded wrong anyway?

like image 59
Pekka Avatar answered Dec 07 '25 01:12

Pekka