Reputation: 49
I'm getting a feed that contains json data that is then decoded by php (json_decode) and echoed back out to my html page.
The feed contains \n
characters but when the PHP echos them out, the HTML doesn't recognize them as new lines or line breaks so the HTML text just comes out as one big wall of text.
Can someone give me a hint as to what I should be doing to make the php echo out a
or something similar when the json data has \n
in it?
As an example, the json data might contain "FRIDAY THROUGH WEDNESDAY.\n\nMORE RAIN IS" but when the HTML is generated it just looks like "FRIDAY THROUGH WEDNESDAY. MORE RAIN IS" all on the same line.
Thanks!
Upvotes: 0
Views: 1057
Reputation: 3206
You must replace your '\n' with the equivalent in HTML, which is the br tag.
You can output your string in the following way :
echo str_replace("\n", "<br />", $yourString);
Upvotes: 1
Reputation: 16027
From php.net:
https://www.php.net/manual/fr/function.json-decode.php#112084
function json_decode_nice($json, $assoc = TRUE){
$json = str_replace(array("\n","\r"),"\\n",$json);
$json = preg_replace('/([{,]+)(\s*)([^"]+?)\s*:/','$1"$3":',$json);
$json = preg_replace('/(,)\s*}$/','}',$json);
return json_decode($json,$assoc);
}
Upvotes: 0