zauber3r
zauber3r

Reputation: 95

PHP Stripslash doesn't work

in my MySQL table I got the comment:

"{"out_user":"pb","out_email":"[email protected]","out_date":"13.04.2015"}"

The code I am using is:

$result = mysqli_query($coni, "SELECT table_comment FROM INFORMATION_SCHEMA.TABLES WHERE table_name='$tablename'");
$row = mysqli_fetch_object($result);
$head = $row->table_comment;
echo json_encode(stripslashes($head));

The output is still:

"\"out_user\":\"pb\",\"out_email\":\"[email protected]\",\"out_date\":\"13.04.2015\"}"

Why doesn't stripslahes work?

Upvotes: 0

Views: 122

Answers (2)

Maybe the correct way is decode and not encode...

var_dump( json_decode( $head ) );

or simply

var_dump( $head );

Upvotes: 0

tadman
tadman

Reputation: 211560

What you're seeing is escaped quotes, where "\"" is a double quote that's quoted. For the language to differentiate between quotes that terminate your string and quotes that are a part of your string the backslash character is necessary. The backslash character is introduced by the JSON encoding, so your stripslashes is operating at the wrong point to grab those.

I think what you're seeing here is you're JSON encoding a pre-existing JSON string for no apparent reason. If you removed the re-encoding part it'd probably produce what you expect.

If you're dealing with already JSON-encoded data, just output it as-is, no mangling or handling required.

Upvotes: 3

Related Questions