简体   繁体   English

将2 \\ n替换为 <br \\> 在PHP中?

[英]Replace 2 \n in text to <br \> in php?

i know the nl2br function in php to replace '\\n' to <br \\> , 我知道php中的nl2br函数将'\\ n'替换为<br \\>

but how to replace \\n\\n to <br \\> . 但是如何将\\ n \\ n替换为<br \\>

for example: 例如:

string=

'paragraph
1

paragraph2'

it is expected to show 它有望显示

paragraph1

paragraph2

ps i have a form to input the text. 附言:我有一种输入文字的表格。 Any help is most appreciated :) 任何帮助是最感激的:)

用一些简单的正则表达式轻松替换它:

preg_replace('/\n+/', '<br/>', $string);

try this 尝试这个

$string=

'paragraph
1

paragraph2';


$arr_temp = explode("\n", $string);
$prev_blank = false;
foreach($arr_temp as $str)
{
    if(trim($str)=="")
    {
        if(!$prev_blank)
        {
            echo "<br/>";
            $prev_blank = true;
        }

    }
    else
    {
        echo trim($str);
        $prev_blank = false;
    }

}

output: 输出:

paragraph1
paragraph2

i've solved the problem. 我已经解决了这个问题。

just use 只是使用

str_replace("\n\r\n", "<br />", $model->solutionText);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM