JSON_Encode Function: Special Characters

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.

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.

json_encode strings with special characters and spaces

Thanks to @deceze in the comments. The attribute had to be in quotes. Also @WPhil is right, the JSON.parse() part was also missing.

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.

json encode - special characters

You should use htmlspecialchars(). This with proper options will make your string to be valid HTML.
json_encode is to get json string from PHP value. You are using this with strings, so in fact you will not want to have json. Particularly I don't see why you use it with post_id.

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.

json_encode adding special characters

On PHP 5.3.13:


header("Content-Type: text/plain"); // to make sure browser displays raw html
$str = '<option value="auto">auto</option>';

echo json_encode($str);
echo "\n";
echo $str;

Outputs

"<option value=\"auto\">auto<\/option>"
<option value="auto">auto</option>

No converted special chars here.

$.post('test.php').done(function(text){
return console.log(text);
});

Prints the same to the console.

Without AJAX does json_encode($str) display " etc. ?



Related Topics



Leave a reply



Submit