English 中文(简体)
缩略语
原标题:Escaping/encoding single quotes in JSON encoded HTML5 data attributes
  • 时间:2012-01-12 09:16:06
  •  标签:
  • php
  • json
  • html

In PHP, I use json_encode() to echo arrays in HTML5 data attributes. As JSON requires - and json_encode() generates - values encapsulated by double quotes. I therefor wrap my data attributes with single quotes, like:

<article data-tags= ["html5","jquery","php","test s"] >

如你所知,最后的标签(试卷)含有单一报价,并且使用<编码>json_encode(<>>/code(>),没有任何选择会导致同化问题。

因此,我使用<条码>json_encode(>> with the JSON_HEX_APOS para amount,而同值一样是罚款,因为我的单一报价被编码了,但我很想知道:是否在这方面有 down?

最佳回答

您需要以超文本方式提取数据,作为超文本:

printf( <article data-tags="%s"> ,
    htmlspecialchars(json_encode(array( html5 , ...)), ENT_QUOTES,  UTF-8 ));
问题回答

或使用建构选择:

json_encode(array( html5 , ...), JSON_HEX_APOS)

可在手册中加以核对:http://php.net/manual/en/json.constants.php#constant.json-hex-'rel=“noreferer” http://php.net/manual/en/json.constants.php#constant.json-hex-“。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签