最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - My webpage display ' and not ' - Stack Overflow

programmeradmin2浏览0评论

I have a php script which return a JSON, and a js function which parse this JSON. In my php I did a htmlspecialchars but when I display value in my webpage ' isn't replace by ' same for " any idea ?

I have a php script which return a JSON, and a js function which parse this JSON. In my php I did a htmlspecialchars but when I display value in my webpage ' isn't replace by ' same for " any idea ?

Share Improve this question edited Apr 28, 2015 at 19:58 Jay Blanchard 34.4k16 gold badges80 silver badges125 bronze badges asked Apr 28, 2015 at 19:57 François MENTECFrançois MENTEC 1,3044 gold badges16 silver badges25 bronze badges 5
  • How are you putting the JSON result into the page? – Barmar Commented Apr 28, 2015 at 20:00
  • Why are you calling htmlspecialchars() in the first place? The usual reason for that is so that you see the literal HTML, instead of getting it parsed. – Barmar Commented Apr 28, 2015 at 20:02
  • If I don't use I have an object and not a value in my JSON I don't know why but the htmlspecialchars solve the problem then I use it – François MENTEC Commented Apr 28, 2015 at 20:11
  • it sounds like there's a problem with how you're creating the JSON. json_encode() shouldn't create an object instead of a value. You REALLY need to show your code for us to help you. – Barmar Commented Apr 28, 2015 at 20:17
  • I just cast to string :$this->description = (string)$description; to solve my problem, and remove the htmlspecialchars – François MENTEC Commented Apr 28, 2015 at 20:25
Add a comment  | 

3 Answers 3

Reset to default 10

If you are seeing ' on the page, you have probably double-encoded your string somewhere along the lines.

Encoding the string the first time changes ' to '.

Encoding the string a second time changes ' to '.

The result of this is that you see the code, not the char - as the web page converts the & to & visually, and ignores the rest.

Use html_entity_decode() with ENT_QUOTES

$string = "test '";
echo html_entity_decode($string, ENT_QUOTES);

Output:

test '

DEMO http://ideone.com/pZdJOa


read more about html_entity_decode

When you do use htmlspecialchars " "'" (single quote) becomes ''' (or ') "

Read the PHP Manual.

发布评论

评论列表(0)

  1. 暂无评论