Hi again, I've been running addslashes() on the input before running json_encode () and it still creates problems when I pass it through AJAX - breaks HTML tags etc. If I turn addslashes() off, any occurence of "" in the body text (eg quotes from speakers etc) breaks the code again. Does anyone have a foolproof method?
Cheers Matt On Apr 24, 9:54 pm, Matt Foster <mattfoste...@gmail.com> wrote: > What are you sending to PHP's json_encode? It is expecting a > structure to serialize into a JSON syntax string. > > $struct = array("message" => "<h1>Hello World</h1>Who says we can't > have any kind of \"quotes\" we want?"); > > echo json_encode($struct); > > If you try to run json_encode on your already JSONified string, yeah > its going to cause problems... > > -- > > http://positionabsolute.net > > On Apr 24, 12:26 pm, "T.J. Crowder" <t...@crowdersoftware.com> wrote: > > > @Ananth: > > > Converting to Base64 would be massive overkill, surely. > > > @OP: > > > There's no reason you can't include strings containing HTML in JSON > > data. You just have to make sure your strings are valid string > > literals, like this: > > > { > > message: "<p>This is HTML.</p>" > > > } > > > There's nothing special other than making sure the HTML is correctly > > escaped -- e.g., if you're putting the JSON string in double quotes, > > naturally any double quotes in the HTML will need a backslash in front > > of them -- as, for that matter, will any backslashes! > > > HTH, > > -- > > T.J. Crowder > > tj / crowder software / com > > > On Apr 24, 5:05 pm, Ananth Raghuraman <araghuram...@gmail.com> wrote: > > > > JSON should not contain HTML for tactical/ease of programming purposes > > > unless the HTML is there as part of a larger design, but there may or may > > > not be implementation restrictions. > > > If you are facing problems, can you try encoding the HTML string (Base64) > > > and decoding back (using Javascript Base64 code ) before display on the > > > browser? > > > > On Fri, Apr 24, 2009 at 11:30 AM, Matt <guitarroman...@gmail.com> wrote: > > > > > Hi there, > > > > > I'm using PHP to output some JSON through AJAX to my page. It breaks > > > > whenever I use a backslash or quote mark. > > > > > I've tried using php's json_encode function which doesn't seem to > > > > help, just breaks it further. I've also tried php's addslashes() to > > > > the output, again, same problem. > > > > > Am I approaching this correctly, fundamentally? Is JSON supposed to > > > > contain HTML? > > > > > Thanks, > > > > > Matt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---