英文:
How can I control single and double quotes in string transfers from PHP to Javascript?
问题
I'm trying to copy strings that have escaped single and double quotes from PHP to JavaScript. When I get the string having escaped quotes on the server into JavaScript the escape back-slashes are gone.
Ultimately I'm trying to transfer JSON from the server to the client JavaScript. The string values in the JSON may have single or double quotes.
I have to use single quotes to denote the string literal on the client because JSON is full of double quotes. That means all single quotes in the string must be escaped.
<?php
$s = "this\'s a test";
echo $s;
?>
<html>
<body>
<script>
console.log('<?= $s?>'); //displays: this's a test
</script>
</body>
</html>
edit per ADyson
The echoed string in PHP (below) shows the escape on the single quote removed.
The double quote is double escaped this time (3 backslashes).
What console.log
(below) is displaying is not valid JSON.
I can't control it.
How can I transfer the object $obj to an identical obj in Javascript?
$str = 'Johnny\'s back yard has a sign that says \"NO SHOUTING!\". We\'re \"NOT\" SHOUTING!';
$obj = (object)["Name"=>"The big Test", "Str" => $str];
$s = json_encode($obj);
echo $s;
?>
<html><body><script>
console.log('<?= $s?>'); //This crashes
var obj = JSON.parse('<?= $s?>'); //This is never executed but would crash.
</script></body></html>
英文:
I'm trying to copy strings that have escaped single and double quotes from PHP to JavaScript. When I get the string having escaped quotes on the server into JavaScript the escape back-slashes are gone.
Ultimately I'm trying to transfer JSON from the server to the client JavaScript. The string values in the JSON may have single or double quotes.
I have to use single quotes to denote the string literal on the client because JSON is full of double quotes. That means all single quotes in the string must be escaped.
<?php
$s = "this\'s a test";
echo $s;
?>
<html>
<body>
<script>
console.log('<?= $s?>'); //displays: this's a test
</script>
</body>
</html>
edit per ADyson
The echoed string in PHP (below) shows the escape on the single quote removed.
The double quote is double escaped this time (3 back slashes).
What console.log (below) is displaying is not valid JSON.
I can't control it.
How can I transfer the object $obj to an identical obj in Javascript?
$str = 'Johnny\'s back yard has a sign that says \"NO SHOUTING!\". We\'re \"NOT\" SHOUTING!';
$obj = (object)["Name"=>"The big Test", "Str" => $str];
$s = json_encode($obj);
echo $s;
?>
<html><body><script>
console.log('<?= $s?>'); //This crashes
var obj = JSON.parse('<?= $s?>'); //This is never executed but would crash.
</script></body></html>
答案1
得分: -1
如果您想从包含JSON的PHP字符串创建一个JS对象,只需使用以下代码:
var obj = <?= $s ?>;
就足够了。请记住,JS可以将JSON读取为对象字面量。JSON语法是JavaScript对象字面量语法的子集。
英文:
If you want to make a JS object from your PHP string which contains JSON, just
var obj = <?= $s?>;
is sufficient. Remember JS can read JSON as an object literal. JSON syntax is a subset of JavaScript object literal syntax.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论