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
Move_Uploaded_File() Function Is Not Working
How to Prevent Pdo from Interpreting a Question Mark as a Placeholder
Static' Keyword Inside Function
Destroy or Unset Session When User Close the Browser Without Clicking on Logout
Utf-8 Safe Equivalent of Ord or Charcodeat() in PHP
Get Element by Classname with Domdocument() Method
Laravel 5 Change Public_Path()
Posting Multidimensional Array with PHP and Curl
Mysqli Query Results to Show All Rows
Cloudflare and Logging Visitor Ip Addresses via in PHP
Why Are Functions and Methods in PHP Case-Insensitive
Storing Form Data as a Session Variable
PHP Flush/Ob_Flush Not Working
Sf2 Form:Error Neither the Property ... Nor One of the Methods "Get