Sending HTML Code Through JSON Sending HTML Code Through JSON mysql mysql

Sending HTML Code Through JSON


Yes, you can use json_encode to take your HTML string and escape it as necessary to be valid JSON (it'll also do things that are unnecessary, sadly, unless you use flags to prevent it). For instance, if your original string is:

<p class="special">content</p>

...json_encode will produce this:

"<p class=\"special\">content<\/p>"

You'll notice it has an unnecessary backslash before the / near the end. You can use the JSON_UNESCAPED_SLASHES flag to prevent the unnecessary backslashes. json_encode(theString, JSON_UNESCAPED_SLASHES); produces:

"<p class=\"special\">content</p>"


Do Like this

1st put all your HTML content to array, then do json_encode

$html_content="<p>hello this is sample text";$json_array=array('content'=>50,'html_content'=>$html_content);echo json_encode($json_array);


All string data must be UTF-8 encoded.

$out = array(   'render' => utf8_encode($renderOutput),    'text' => utf8_encode($textOutput));$out = json_encode($out);die($out);