Display html code in json and php

Hi guys

My current code is

$responseData = ['success' => true, 'message' => '<b>Thankyou for booking a viewing.</b><br>We will be in touch within 24 hours to confirm your appointment.'];
echo json_encode($responseData);

But its outputting exactly

<b>Thankyou for booking a viewing.</b><br>We will be in touch within 24 hours to confirm your appointment.

Whereas I want the text to be bold etc…

Ive tried a few things like htmlentities() etc and cant get this working. What am I doing wrong?
Thanks

I really don’t know what “etc…” is, but you have the bold tags round half the text. If you want the entire text bold then put the bold tags round the entire text.

(You would be better off using strong rather than b though.)

Thanks @Gandalf . The ‘etc…’ was meant to mean the ‘line break’. I should have just said that! :slight_smile:

I notice that in my post - after “But its outputting exactly” - after its outputting the text as I want it (ie formatted html) whereas currently its outputting the html tags as html tags.

Gotcha! I have edited your original post. Hopefully it now explains the problem correctly.

The thing is that HTML code in a post is taken literally unless you escape it or enclose it in backticks.

2 Likes

What happens if you

echo $responseData['message'];

That doesnt display anything.
The code Ive attached is obviously part of a much bigger part of code, so possibly the issue is with another part of code.
But am I right in thinking that

$responseData = ['success' => true, 'message' => '<b>Thankyou for booking a viewing.</b><br>We will be in touch within 24 hours to confirm your appointment.'];
echo json_encode($responseData);

should echo the message as formatted html? Or do I need an additional php function. Something like:

$messageDisplay = htmlentities("<b>my text</b><br>new line")
$responseData = ['success' => true, 'message' => $messageDisplay];
echo json_encode($responseData);

I wouldn’t expect that - I would expect it to echo a JSON-encoded version of the array that you created. That’s why I thought that echoing it without the jsonencode() might do it. Saying that, those two lines don’t produce the output that you put in your first post. When I run those two lines on phpfiddle, I get:

Capture

But when I echo as I posted, I get

Capture

I can only presume there is other code involved that is making a difference.

Not sure about the second version, what happens when you try it? I don’t think I’ve used htmlentities() myself, but from the doc it seems to be the reverse of what you want.

Hmm. OK, the site is using AngularJS (Ive inherited this website so still groping around regards how its been built).
I know nothing of Angular :frowning:

Ive found the line of code which output the msg to the browser. It is:

<p ng-class="result" style="padding: 15px; margin: 0;">{{ resultMessage }}</p>

Perhaps there is an Angular function which forces it to be html formatted. (Im googling but cant find anything yet)

Personally, I wouldn’t use HTML in the message since it’ll just screw everything up if someone were to happen to view the plain JSON file. I’d just use markdown and later convert the markdown into readable HTML. Something like so

$responseData = ['success' => true, 'message' => "**Thankyou for booking a viewing.** \r\nWe will be in touch within 24 hours to confirm your appointment."]; echo json_encode($responseData);

Then when I would just need a markdown parser library to convert the markdown into generic HTML. I use erusev’s markdown parser so I can just do something like

print $markdown->Text($jsonObject);

Which then turns the message into HTML.

4 Likes

I think the problem is from json_encode escaping slashes by default, so you end up with <\/b> in your closing b tag.
You can stop this with the JSON_UNESCAPED_SLASHES option, but I would agree, it’s probably best to keep HTML out of your json data.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.