dtnmuyoiw680512744 2016-10-31 19:02
浏览 53

nl2br()函数广告<br>如果在/下一行之后没有输入字符[复制]

This question already has an answer here:

I am using nl2br function with str_replace as follows:

$data = (!empty($_POST['content']))?nl2br($_POST['content']):null;
$data_fix = str_replace(array("", "
"), '', $data);

When I insert data into database next lines automatically receives a <br>. Okay fine. To 95% extent this is what I want. But if there is no character entered after the next line in <textarea> it still enters a <br>.

Example:

What I want:

Hello Stackoverflow!<br>I am new here.
(new line space eliminated)

What I get:

Hellow Stactoverflow!<br>I am new here.<br>
(new line space inserted)

Please help me solve this!

</div>
  • 写回答

1条回答 默认 最新

  • doujianwan7570 2016-10-31 19:17
    关注

    trim() function will prevent adding extra <br> which are not needed if there is no text entered in or after the next line!

    Simply replaced

    $data = (!empty($_POST['content']))?nl2br($_POST['content']):null;
    

    With

    $data = (!empty($_POST['content']))?nl2br(trim($_POST['content'])):null;
    

    And it did the job! But trim() should be after the nl2br() otherwise it won't work!

    Answered my own question for future reference of other people seeking solution to the same!

    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计