The first way is a function trim()before blowing up a string.
$text = trim($text, "\n");
$text = explode( "\n", $text );
foreach( $text as $str ) { echo $str; }
Another way could be use array_filter()after an explosion.
$text = explode( "\n", $text );
$text = array_filter($text);
foreach( $text as $str ) { echo $str; }
By default, it array_filter()removes elements that are equal false, so there is no need to define a callback as a second parameter.
Anyway, I think the best way is best here.
source
share