JSON_Encode Not Working with a HTML String as Value

json_encode does not display HTML

The problem is your use of jQuery's .text():

$(".call-conteudo").text(data['conteudo_programatico']);
// etc.

According to the manual:

We need to be aware that this method escapes the string provided as
necessary so that it will render correctly in HTML. To do so, it calls
the DOM method .createTextNode(), does not interpret the string as
HTML

...

The code $( "div.demo-container" ).text( "<p>This is a test.</p>" );
will produce the following DOM output:

 1 <div class="demo-container">
2 <p>This is a test.</p>
3 </div>

You need html():

$(".call-conteudo").html(data['conteudo_programatico']);
// etc.

json_encode() can't return html

Try this:

<?php echo json_encode($snippetData, JSON_HEX_QUOT | JSON_HEX_TAG);

Source

How do I JSON Encode HTML in PHP?

Encoding the html like this seems to add a \ before every / causing
the html to break

PHP is escaping the slashes while encoding. This can be preventing by adding a JSON_UNESCAPED_SLASHES flag when calling json_encode():

$data = "<html></html>";

$escaped = json_encode($data);
// string(16) ""<html><\/html>""
var_dump($escaped);

$unescaped = json_encode($data, JSON_UNESCAPED_SLASHES);
// string(15) ""<html></html>""
var_dump($unescaped);

php JSON_encode not working

Thanks to @awiebe I found the exact error. It's

Malformed UTF-8 characters, possibly incorrectly encoded

Thank you all, I found a solution from an another question.
'Malformed UTF-8 characters, possibly incorrectly encoded' in Laravel

json_encode not showing anything in my external javascript

try in your php page first set $ab to value to javascript variable then include your js

echo "<script>var db_array = '". json_encode($ab)."';</script>
<script type='text/javascript' src='javascript1.js'></script>";

and alert in your js file

Json_Encode with Html String in Twig

This solution work fine for me:

{{ '<h5>Name</h5>'|json_encode(constant('JSON_UNESCAPED_SLASHES')) }}

Having trouble using php json_encode on local server

If you use Apache, then you can change the httpd.conf file and add or change (if it's already there) to:

    AddDefaultCharset utf-8

Json encoding is not working properly

First off: You're converting HTML by regex. That is incredibly bad/stupid and WILL turn around and bite you in the rump.

That being said, since you ARE converting HTML and then json_encoding the resulting array, you'll get just HTML-in-a-string, which your browser is going to render, e.g.

$arr = array(
'<p>This is a paragraph</p>',
'<div>this div has an <span>in-line span</span></div>'
);

echo json_encode($arr);

which will give you a raw JSON string of

["<p>This is a paragraph<\/p>","<div>this div has an <span>in-line span<\/span><\/div>"]

Viewed in a browser in default html-rendering mode, you'll end up with

["
This is a paragraph<\/p>
","
This div has an in-line span<\/span><\/div>"]

Note how the tags are "removed" - because the browser rendered them. Do a view source of your page, and you'll see your tags. Also note that the closing tags were NOT rendered, because the/` was escaped by JSON, turning them into "invalid" html tags.



Related Topics



Leave a reply



Submit