简体   繁体   中英

PHP Remove Unused Line Breaks Using nl2br

How to remove unused line breaks using nl2br function for this example:

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.

Here is my PHP code:

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;

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