Failed to decode JSON stripslashed String?

Does anyone know why this is happening?

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 'JSON functions used?

+3
source share
3 answers

I do not know for sure, but json_last_error () should :)

My assumption, however, is that it json_encode()does something with \', which then interrupts stripslashes()- for example. add another "\" to avoid backslash.

Don't mess around with a json encoded string using striplslashes()before you decode anyway?

+5
source

I did not look too deep, but it looks like your code

  • Taking a PHP array and turning it into a json string

  • json

:

$json_string = json_encode(array("O\'Reiley");
$json_string = stripslashes($json_string);      

//it no longer json, its just some random non-conforming string
var_dump(json_decode($json_string))
+2

stripslashes()

$result = json_encode(striptslashes(array("O\'Reiley")));
if(json_last_error() > 0){
   $result = json_encode(array("O\'Reiley"));
}
0

Source: https://habr.com/ru/post/1728782/


All Articles