问题
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?
回答1:
From http://www.php.net/manual/en/function.json-encode.php#100565
That said, quotes " will produce invalid JSON, but this is only an issue if you're using json_encode() and just expect PHP to magically escape your quotes. You need to do the escaping yourself.
回答2:
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');
回答3:
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 ));
回答4:
Use json_encode($json_array, JSON_HEX_QUOT);
since php 5.3: http://php.net/manual/en/json.constants.php
回答5:
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>
回答6:
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');
回答7:
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.
来源:https://stackoverflow.com/questions/7462394/php-json-string-escape-double-quotes-for-js-output