在 PHP 中使用 nl2br()
Posted
技术标签:
【中文标题】在 PHP 中使用 nl2br()【英文标题】:Using nl2br() in PHP 【发布时间】:2017-08-09 19:52:54 【问题描述】:我正在使用下面的代码。
<?
echo nl2br( 'The situation deteriorated into attacks on New York elite, followed by attacks on black New Yorkers and their property after fierce competition for a decade between
Irish immigrants and black people for work. Rioters burned the Colored Orphan Asylum to the ground.' , false ) ;
?>
我得到如下输出
The situation deteriorated into attacks on New York elite, followed by attacks on black New Yorkers and their property after fierce competition for a decade between <br>
Irish immigrants and black people for work. Rioters burned the Colored Orphan Asylum to the ground.
我想在没有<br>
的情况下获得输出,但此时我需要换行符(新行)。
【问题讨论】:
nl2br 的全部目的是添加 ...如果您使用的是 nl2br,那么您有一个换行符。您可以使用“\n”在引号内插入换行符。 Blade: escaping text and allowing new lines的可能重复 【参考方案1】:你为什么不能这样做..
<?
echo "The situation deteriorated into attacks on New York elite, followed by attacks on black New Yorkers and their property after fierce competition for a decade between\n Irish immigrants and black people for work. Rioters burned the Colored Orphan Asylum to the ground.";
?>
【讨论】:
有效吗?是的..回答..确定..他想要的答案..不确定..我们会看到他说什么.. 为什么不呢?我可以看看你的代码吗?你有 2 个选择.. 换行 () 或换行 (\n).. 你不想要换行。 谢谢@Rick。我正在尝试echo
blade
Laravel
的模板文件中的输出。这是代码<div class="col-xs-12">nl2br($story->note)</div>
。谢谢
对不起..我只是没有关注..您是否希望将文本字符串分成 2 行?
我想把那串文本分成两行。【参考方案2】:
试试这个。顺便说一句,我不知道字符串后面的“false”是什么,但如果你在代码中删除它,它工作得很好,我看不到 html 换行标记。
<?php
$string = 'The situation deteriorated into attacks on New York elite,followed by attacks on black New Yorkers and their property after fierce competition for a decade between Irish immigrants and black people for work. Rioters burned the Colored Orphan Asylum to the ground.';
function nl2br2($string)
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
return $string;
echo"$string";
?>
【讨论】:
以上是关于在 PHP 中使用 nl2br()的主要内容,如果未能解决你的问题,请参考以下文章