【问题标题】:php look for new line in a varible and insert <br> [duplicate]php 在变量中查找新行并插入 <br> [重复]
【发布时间】:2018-11-06 15:06:01
【问题描述】:

我有一个像这样的变量:

$Text= "Hello 
        My 
        Code"

所以我想要发生的是在哪里有一个新行要插入
所以它应该看起来像

$NewText = "Hello<br>
            My<br> 
            Code"

*

【问题讨论】:

  • 你自己尝试过什么吗?

标签: php regex


【解决方案1】:

您可以使用nl2brpreg_replacestr_replace

$text = "Hello\nMy\nCode";

$newText = nl2br($text);

$newText = preg_replace('/\r?\n/', "<br>\n", $text);

$newText = str_replace("\n", "<br>\n", $text);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 2023-02-02
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多