$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!
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.
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:
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:
But when I echo as I posted, I get
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.
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
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.