【问题标题】:Using nl2br() in PHP在 PHP 中使用 nl2br()
【发布时间】: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.

我想在没有&lt;br&gt; 的情况下获得输出,但此时我需要换行符(新行)。

【问题讨论】:

标签: php string laravel blade nl2br


【解决方案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。我正在尝试echobladeLaravel 的模板文件中的输出。这是代码&lt;div class="col-xs-12"&gt;{{nl2br($story-&gt;note)}}&lt;/div&gt;。谢谢
  • 对不起..我只是没有关注..您是否希望将文本字符串分成 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";
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 2015-09-23
    • 2015-04-18
    • 2014-08-07
    • 1970-01-01
    相关资源
    最近更新 更多