简体   繁体   English

PHP使用nl2br删除未使用的换行符

[英]PHP Remove Unused Line Breaks Using nl2br

How to remove unused line breaks using nl2br function for this example: 对于本示例,如何使用nl2br函数删除未使用的换行符:

Hello


Nice

Expect output display: 预期输出显示:

Hello

Nice

and another example: 再举一个例子:

remove this unused line
remove this unused line
remove this unused line
Hello
remove this unused line
remove this unused line
remove this unused line
remove this unused line

Expect output display: 预期输出显示:

Hello

So means if the line break more than 3 line, so only set 1 line breaks. 因此意味着如果换行超过3行,则仅设置1个换行。

Here is my PHP code: 这是我的PHP代码:

nl2br($string);

Old schooled but hope this works 受过高等教育,但希望能奏效

$str = explode("\n", $str);

foreach($str as $val){   
    if(!empty($val)){        
       $result[] = $val; 
    }    
}

$final = implode("\n", $result); //if you want line break use "<br>"
echo $final;

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

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