首页 - PHP编程

html中input的value内容双引号显示问题

html
  1. {"username":"test","age":18}

 需要对双引号部分进行编码才能正常展示。编码后浏览器会自动解释出来。

编码字符列表:

&:转换为&

":转换为"

':转换为成为 '

<:转换为&lt;

>:转换为&gt;

编码结果:

html
  1. {&quot;username&quot;:&quot;test&quot;,&quot;age&quot;:18}
  2.  
  3. 内容:<input type="text" value="{&quot;username&quot;:&quot;test&quot;,&quot;age&quot;:18}" />

解决方式:

js编码内容:

js
  1. str = '{"username":"test","age":18}';
  2. str = str.replace(/&/g,"&amp;");
  3. str = str.replace(/</g,"&lt;");
  4. str = str.replace(/>/g,"&gt;");
  5. str = str.replace(/\s/g,"&nbsp;");
  6. str = str.replace(/\'/g,"&#39;");
  7. str = str.replace(/\"/g,"&quot;");

php编码内容:

php
  1. $str = '{"username":"test","age":18}';
  2. $str = htmlspecialchars($str);