Php's JSON_Encode Does Not Escape All JSON Control Characters

PHP's json_encode does not escape all JSON control characters

D'oh - you need to double-encode: JSON.parse is expecting a string of course:

<script type="text/javascript">

JSON.parse(<?php echo json_encode($s) ?>);

</script>

PHP JSON Special Characters

Change your code to:

header('Content-Type: application/json; charset=utf-8', true,200);
$JSON["today"]=array();

for ($i=0; $i < count($olay_tarih); $i++) {
$gelen["date"]=array();
$gelen["content"]=array();
array_push($gelen["date"], $olay_date[$i]);
array_push($gelen["content"], $olay_content[$i]);
array_push($JSON["today"], $gelen);
}
$JSON = array_map('utf8_encode', $JSON);
echo json_encode($JSON);

Adding UTF-8 headers will make the browser recognize any special characters for this setting.

PHP not escaping when using json_encode

The slashes added by json_encode are escaped for JSON. The database interprets these as escapes for the database and so the slashes do not persist in the data unless they are also escaped. Use a prepared statement:

$_POST = json_encode($_POST);
$query = "REPLACE INTO `$form` (`jsonresults`) VALUES (:json)";
$stm = $pdo->prepare($query);
$stm->bindParam(':json', $_POST, PDO::PARAM_STR);
$stm->execute();

json_encode function: special characters

Your input has to be encoded as UTF-8 or ISO-8859-1.

http://www.php.net/manual/en/function.json-encode.php

Because if you try to convert an array of non-utf8 characters you'll be getting 0 as return value.


Since 5.5.0 The return value on failure was changed from null string to FALSE.

json_encode escape special characters

The way I suggest you deal with anything that goes to html is:

<button onclick='callFunction(<? echo htmlentities(json_encode($myArray),ENT_QUOTES); ?>)'></button>

Check all available flags at http://php.net/manual/en/function.htmlentities.php

This one uses the flag ENT_QUOTES because the default behaviour is to only encode double quotes. Using ENT_QUOTES will also encode single quotes.

Handling special characters in json_encode

This library saved my life(https://github.com/neitanod/forceutf8).

Previously I was using the wrong method to get the issue resolved.
toUTF8() function was life saver.

php json_encode not escaping new lines

I figured out this issue. it is not in json_encode problem. it is an error while saving to database.

The problem is magic_quotes_gpc enabled in server. in my application if magic_quotes enabled i am striping the slashes.

i disabled magic_quotes_gpc. working fine now.

Thanks for every body.



Related Topics



Leave a reply



Submit