I'm creating a JSON string from a PHP array. I've encoded it using json_encode()
.
$data = array(
'title' => 'Example string\'s with "special" characters'
);
$data = json_encode( $data );
$data
is localized using wp_localize_script()
and is accessible via a global data
variable.
In the JS file I can access the information by the following:
var data = data.replace( /"/g, '"' ),
jsonData = jQuery.parseJSON( data );
console.log( jsonData );
This results in an output of:
{ "title":"Example string's with "special" characters" }
Entering that result into http://jsonlint.com/ returns an error. Removing the double quotes around "special" validates the string.
What is the best way to create a JSON string from PHP and properly escape it for use in a JS file?
This question is related to
php
escaping
json
double-quotes
I just ran into this problem and the actual issue was that I forgot to add a proper application/json header before spitting out the actual JSON data.
header('Content-Type: application/json');
This is a solutions that takes care of single and double quotes:
<?php
$php_data = array("title"=>"Example string's with \"special\" characters");
$escaped_data = json_encode( $php_data, JSON_HEX_QUOT|JSON_HEX_APOS );
$escaped_data = str_replace("\u0022", "\\\"", $escaped_data );
$escaped_data = str_replace("\u0027", "\\'", $escaped_data );
?>
<script>
// no need to use JSON.parse()...
var js_data = <?= $escaped_data ?>;
alert(js_data.title); // should alert `Example string's with "special" characters`
</script>
Another way would be to encode the quotes using htmlspecialchars:
$json_array = array(
'title' => 'Example string\'s with "special" characters'
);
$json_decode = htmlspecialchars(json_encode($json_array), ENT_QUOTES, 'UTF-8');
I succefully just did this :
$json = str_replace("\u0022","\\\\\"",json_encode( $phpArray,JSON_HEX_QUOT));
json_encode()
by default will escape "
to \"
. But it's still wrong JSON for json.PARSE()
. So by adding option JSON_HEX_QUOT
, json_encode()
will replace "
with \u0022
. json.PARSE()
still will not like \u0022
. So then we need to replace \u0022
with \\"
. The \\\\\"
is escaped \\"
.
NOTE : you can add option JSON_HEX_APOS
to replace single quote with unicode HEX
value if you have javascript single quote issue.
ex: json_encode( $phpArray, JSON_HEX_APOS|JSON_HEX_QUOT ));
I had challenge with users innocently entering € and some using double quotes to define their content. I tweaked a couple of answers from this page and others to finally define my small little work-around
$products = array($ofDirtyArray);
if($products !=null) {
header("Content-type: application/json");
header('Content-Type: charset=utf-8');
array_walk_recursive($products, function(&$val) {
$val = html_entity_decode(htmlentities($val, ENT_QUOTES, "UTF-8"));
});
echo json_encode($products, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
}
I hope it helps someone/someone improves it.
Use json_encode($json_array, JSON_HEX_QUOT);
since php 5.3: http://php.net/manual/en/json.constants.php
Error come on script not in HTML tags.
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<pre><?php print_r($_POST??'') ?></pre>
<form method="POST">
<input type="text" name="name"><br>
<input type="email" name="email"><br>
<input type="time" name="time"><br>
<input type="date" name="date"><br>
<input type="hidden" name="id"><br>
<textarea name="detail"></textarea>
<input type="submit" value="Submit"><br>
</form>
<?php
/* data */
$data = [
'name'=>'loKESH"'."'\\\\",
'email'=>'[email protected]',
'time'=>date('H:i:00'),
'date'=>date('Y-m-d'),
'detail'=>'Try this var_dump(0=="ZERO") \\ \\"'." ' \\\\ ",
'id'=>123,
];
?>
<span style="display: none;" class="ajax-data"><?=json_encode($_POST??$data)?></span>
<script type="text/javascript">
/* Error */
// var json = JSON.parse('<?=json_encode($data)?>');
/* Error solved */
var json = JSON.parse($('.ajax-data').html());
console.log(json)
/* automatically assigned value by name attr */
for(x in json){
$('[name="'+x+'"]').val(json[x]);
}
</script>
Source: Stackoverflow.com