0

i have a case where i have to send an html code from client side to server side through json object ,but there are some problems caused by symbols inside the html tag such as ",/ ...etc which create errors in the json object. I tried many ways to build the json but none of them were correct .I hop you understand my case clearly . this is an example of my json structure:

{
   "html":"the html code injected dynamical here"
}

as i said my main problem is that i am having problems caused by html symbols how can i solve it! please give me an explicit example on how to solve this problem and pars the html code successfully to json object.

5 Answers 5

2

I'm not sure about how you're building the JSON objects but if you're using json_encode() and json_decode() you shouldn't have any problems with HTML characters, they'll be escaped properly.

Sign up to request clarification or add additional context in comments.

Comments

1

Not sure If I misread the question, but to pass json from server to client, use json_encode()

http://php.net/manual/en/function.json-encode.php

This should work for you and take care of all the escaping.

<?php
header('Content-type: application/json');
$json = array(
    'html'    =>    '<h1 style="color:#0F0">Hello World!</h1>'
);
echo json_encode($json);
exit;

Hmm, just learned this tidbit from reading the docs:

json_encode() won't work with character sets other than UTF-8

I can't see why you'd need to send json to the server, maybe you can share the reason with us or someone can enlighten me in the comments?

1 Comment

if you were working in another charset you'd have to use something like iconv to do some charset conversion
0

Perhaps you could use url encoding.

1 Comment

how ! could you please give me an example of doing that :).take on mined that i give you a simple structure just for the seek of simplicity but my actual structure contains more than html code . i tried the encoding to pay pass these character but it doesn't help me i red some thing about replacing these character by adding special character to be passed when passed to json. also we are using this function frequently and the html created dynamical.
0

You can encode the html. Some html encoding and decode can be find here: http://code.google.com/p/jsool/source/browse/jsool-site/js/util/Encoder.js?r=176

You just need the html.encode and html.decode functions, that way you can do something like

function html_encode(string){
    return string.replace(/./g,function(chr, index){
            return chr.match(/[\w\d]/) ? chr : "&#"+chr.charCodeAt(0)+";" ;
    });
},
function html_decode(string){
    return string.replace(/&#[0-9]+;/g,function(text,index){
            return String.fromCharCode(text.match(/[0-9]+/)[0]);
    });
}

That way you can do

var html = html_encode(myhtml);
{"html": html}

to encode and recover the values using html_decode().

PS.: Sry, i didnt tested out the html_encode and decode functions, but its easy to find some at google.

Comments

0

If you are using jQuery this js library might help. http://www.rahulsingla.com/blog/2010/05/jquery-encode-decode-arbitrary-objects-to-and-from-json

In your javascript before you send the request do this:

var thing = {plugin: 'jquery-json', other: "blah-blah"};
var encoded = $.JSON.encode(thing);

Then after you send the request to php, do this to decode:

var thing = json_decode($_POST['thing']);

You can read more on this here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.