如何在从PHP传递到JavaScript的字符串转换中控制单引号和双引号?

huangapple go评论67阅读模式
英文:

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?

PHP Sandbox code below test

$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.

&lt;?php 
  $s = &quot;this\&#39;s a test&quot;;
  echo $s;
?&gt;
&lt;html&gt;
  &lt;body&gt;
    &lt;script&gt;
      console.log(&#39;&lt;?= $s?&gt;&#39;); //displays: this&#39;s a test
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

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?

PHP Sandbox code below test

$str = &#39;Johnny\&#39;s back yard has a sign that says \&quot;NO SHOUTING!\&quot;. We\&#39;re \&quot;NOT\&quot; SHOUTING!&#39;;
$obj = (object)[&quot;Name&quot;=&gt;&quot;The big Test&quot;, &quot;Str&quot; =&gt; $str];
$s = json_encode($obj);
echo $s;
?&gt;
&lt;html&gt;&lt;body&gt;&lt;script&gt;
console.log(&#39;&lt;?= $s?&gt;&#39;);  //This crashes
var obj = JSON.parse(&#39;&lt;?= $s?&gt;&#39;); //This is never executed but would crash.
&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;

答案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 = &lt;?= $s?&gt;; 

is sufficient. Remember JS can read JSON as an object literal. JSON syntax is a subset of JavaScript object literal syntax.

huangapple
  • 本文由 发表于 2023年7月13日 21:52:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定