5

I'm sending a JSON encoded response to the request coming from iPhone. In few of the values some HTML entities are present. I tried using stripslashes() and html_entity_decode() on such values. In browser I'm able to get the proper JSON response i.e. without these HTML entities but when the same response is seen on iPhone or iPhone simulator the HTML entities displayed again.

How should I resolve this issue? Can someone please help?

If you wan I can provide you the necessary code.

Thanks.

Wolfram
  • 8,044
  • 3
  • 45
  • 66
PHPLover
  • 1
  • 51
  • 158
  • 311
  • 3
    We definitely need an [MCVE](https://stackoverflow.com/help/mcve) to debug it, but it sounds like it might be a caching issue. – Alexander O'Mara Feb 08 '15 at 15:59
  • 1
    Just make sure that you have set the header document type to `header('Content-Type: application/json');` if not. But it is good if you can provide the code. – Pieter P. Feb 12 '15 at 04:27
  • 1
    Show us what is being sent to the phone. Also a link to a screenshot of the phone or something would help – greg_diesel Feb 13 '15 at 22:14
  • Are you running those functions on individual values prior to json encoding as opposed to running them on the whole json string (post-encoding). If doing it after, that is wrong. There's not enough information in the question for us to answer it. – Luke Cousins Feb 14 '15 at 13:24

3 Answers3

3

What about using http://php.net/manual/en/function.strip-tags.php?

This function tries to return a string with all NULL bytes, HTML and PHP tags stripped from a given str

Andrii Mishchenko
  • 2,626
  • 21
  • 19
2

in PHP

<?php
header('Content-Type: application/json');
echo json_encode(array('test' => html_entity_decode("Hello &#8211; World", ENT_COMPAT, 'UTF-8')));

Output:

{"test":"Hello \u2013 World"}

in JS:

var o = jQuery.parseJSON('{"test":"Hello \u2013 World"}');
alert( o.test );

Output:

Hello – World
Simone Nigro
  • 4,717
  • 2
  • 37
  • 72
1

check out https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString+HTML.m from Converting & to & in Objective-C

A NSString category called "GTMNSString+HTML" written by Google works too. Check it out here: gist.github.com/takuma104/ntlniph/blob/master/gtm/Foundation/GTMNSString+HTML.h & here: gist.github.com/takuma104/ntlniph/blob/master/gtm/Foundation/GTMNSString+HTML.m