简体   繁体   中英

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

i know the nl2br function in php to replace '\\n' to <br \\> ,

but how to replace \\n\\n to <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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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