1

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);
HiDayurie Dave
  • 1,791
  • 2
  • 17
  • 45

1 Answers1

2

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;
ASR
  • 1,801
  • 5
  • 25
  • 33
  • tried the code but the display like this : Hello Nice. What I want is Hello (then give it only 1
    Nice like example above
    – HiDayurie Dave Jun 10 '16 at 04:32
  • if you want line break use '
    ' instead of "\n" when implode. when you use "\n" you can see the new line in viewsource
    – ASR Jun 10 '16 at 04:34
  • Or is this you want? `$final = implode("

    ", $result);`
    – ASR Jun 10 '16 at 04:37