doqau82086 2012-01-16 18:23
浏览 13
已采纳

捕获字符串中的新行

I know for sure that this was already been asked before but I just googled around and couldn't find anything (maybe wrong word choice?). Just don't be too mad at me, I'm getting mad too...

I'd like

$string = '
This is a line
This is another line
';

to be shown to the HTML page as

This is a line
This is another line

when I do echo $string;. How can I capture the return key or the new line and replace it with <br>?

  • 写回答

5条回答 默认 最新

  • dpl3350 2012-01-16 18:24
    关注

    Try the nl2br() function:

    echo nl2br($string);
    

    Would return:

    <br>
    This is a line<br>
    This is another line<br>
    

    To trim off the leading and trailing new lines, use trim():

    echo nl2br(trim($string));
    

    Would return:

    This is a line<br>
    This is another line
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失