9

I send json_encoded data from my PHP server to iPhone app. Strings containing html entities, like '&' are escaped by json_encode and sent as &.

I am looking to do one of two things:

  • make json_encode not escape html entities. Doc says 'normal' mode shouldn't escape it but it doesn't work for me. Any ideas?

  • make the iPhone app un-escape html entities cheaply. The only way I can think of doing it now involves spinning up a XML/HTML parser which is very expensive. Any cheaper suggestions?

Thanks!

iDhaval
  • 7,806
  • 2
  • 18
  • 30
psychotik
  • 35,551
  • 33
  • 95
  • 134

4 Answers4

8

Neither PHP 5.3 nor PHP 5.2 touch the HTML entities.

You can test this with the following code:

<?php
header("Content-type: text/plain"); //makes sure entities are not interpreted
$s = 'A string with &amp; &#x6F8 entities';
echo json_encode($s);

You'll see the only thing PHP does is to add double quotes around the string.

Artefacto
  • 90,634
  • 15
  • 187
  • 215
3

json_encode does not do that. You have another component that is doing the HTML encoding.

If you use the JSON_HEX_ options you can avoid that any < or & characters appear in the output (they'd get converted to \u003C or similar JS string literal escapes), thus possibly avoiding the problem:

json_encode($s, JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_QUOT)

though this would depend on knowing exactly which characters are being HTML-encoded further downstream. Maybe non-ASCII characters too?

bobince
  • 498,320
  • 101
  • 621
  • 807
2

Based on the manual it appears that json_encode shouldn't be escaping your entities, unless you explicitly tell it to, in PHP 5.3. Are you perhaps running an older version of PHP?

kander
  • 4,086
  • 1
  • 19
  • 41
0

Going off of Artefacto's answer, I would recommend using this header, it's specifically designed for JSON data instead of just using plain text.

<?php
header('Content-Type: application/json'); //Also makes sure entities are not interpreted
$s = 'A string with &amp; &#x6F8 entities';
echo json_encode($s);

Make sure you check out this post for more specific reasons why to use this content type, What is the correct JSON content type?

Community
  • 1
  • 1
Tom
  • 1,207
  • 1
  • 16
  • 26