字符串中的HTML中断标记在浏览器中显示为<br/> <br/>,但也作为<br/>开发控制台

时间:2016-08-18 09:10:16

标签: php html laravel

解决了问题!

我在项目中遇到了一个小问题,我正在从API中加载数据,这会返回一个json对象。一些字符串在字符串中包含<br>标记,当我尝试输出它们时,它们在浏览器中显示为文字中断标记。我打开了Chrome开发工具,它们也显示为<br>,而不是&lt;br&gt;,但它们位于双引号内(")。希望下面的图片解释我的意思。我对这个问题的猜测是<div>标签内的信息被格式化为字符串。提前谢谢!

这是我正在谈论的json对象

"lore":"Most men would say that death is a thing to be feared; none of those men would be Olaf. The Berserker lives only for the roar of a battle cry and the clash of steel. Spurred on by his hunger for glory and the looming curse of a forgettable death, Olaf throws himself into every fight with reckless abandon. Surrendering to the bloodlust deep within his being, Olaf is only truly alive when grappling with the jaws of death.<br><br>The coastal peninsula of Lokfar is among the most brutal places in the Freljord. There, rage is the only fire to warm frozen bones, blood is the only liquid that flows freely, and there is no worse fate than to grow old, frail, and forgotten. Olaf was a warrior of Lokfar with no shortage of glories and no hesitation to share them. While boasting one evening with his clansmen over the burning embers of a razed village, one of the elder warriors grew tired of Olaf's bluster. The old fighter goaded Olaf to read the omens and see if Olaf's fortunes matched his gloating. Emboldened by the challenge, Olaf mocked the aged raider's envy and tossed the knuckle bones of a long-dead beast to predict the heights of glory he'd achieve in death. All mirth left the gathering as the clansmen read the portents: the bones spoke of a long life and a quiet passing.<br><br>Infuriated, Olaf stormed into the night determined to prove the prediction false by finding and slaughtering Lokfar's feared frost serpent. The monster had consumed thousands, man and ship alike, in its long lifetime and to die in battle with it would be a fitting end for any warrior. As Olaf hurled himself into the blackness of its maw, he fell deeper into the blackness of his mind. When the shock of freezing water roused him from the dark, there was only the butchered carcass of the beast afloat beside him. Thwarted but not defeated, Olaf set out to hunt down every legendary creature with claws and fangs, hoping that the next battle would be his last. Each time he charged headlong toward his coveted death, only to be spared by the frenzy that washed over him while on its brink.<br><br>Olaf concluded that no mere beast could grant him a warrior's death. His solution was to take on the most fearsome tribe in the Freljord: the Winter's Claw. Sejuani appeared amused by Olaf's challenge to her warband, but his audacity would earn him no mercy. She ordered the charge and sent scores of her warriors to overwhelm Olaf. One by one, they fell until he lost himself in the bloodlust once again, effortlessly cutting a path to the leader of the Winter's Claw. The clash between Olaf and Sejuani rocked the glaciers with its force, and though he seemed unstoppable, Sejuani battled the berserker to a standstill. As they stood deadlocked, Sejuani's glare penetrated Olaf's berserker haze in a way no weapon ever could. His frenzy abated long enough for her to make him an offer: Sejuani swore that she would find Olaf his glorious death if he would lend his axe to her campaign of conquest. In that moment, Olaf vowed he would carve his legacy into the Freljord itself.<br><br>''When you meet your ancestors, tell them Olaf sent you.'' <br>-- Olaf"

这是使用json_decode($json, true)解码的,因此上面的json存储为字符串,我使用laravel输出,如:

$lore = json_decode($json, true);

<div>{{ $lore }}</div>

enter image description here

更新:我在变量到达Laravel刀片视图之前对变量进行了var_dumped,它运行得很好,用较小的段落表示,即<br>标签不是文字的。一旦它们到达刀片视图,断点标记就会显示为字面值。使用print_r($champion->lore)中断标记再次起作用,不可见,但打破了界限。只有当返回值为false时,它似乎无法使用print_r($champion->lore, true)。 ($champion->lore只是输出的字符串)

任何可以帮助我的人以及为什么会这样?

更新2:我意识到这个问题在Laravel Blade Views中是我的错误。我使用{{ }}输出(echo)字符串,但是带有双花括号的东西是它们转义字符串,即<br>变为&lt;br&gt;。使用{!! !!}解决了问题!

2 个答案:

答案 0 :(得分:2)

您没有查看源代码。您正在查看DOM检查器中的可视化。它将字符串显示为原始字符串(即 text 而非HTML),以便于阅读。

答案 1 :(得分:0)

我不确定你是如何打印你的json值但是你可以只使用replace来避免<br>

json = json.replace("<br>", " ");

也可能你必须使用正则表达式(你可以google这个)。

您可以关注:how to remove <p> , </p> and <br /> which are obtained by parsing JSON response