Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I get data from a textarea where a user has to enter a name one on each line. That data later gets split at the carriage return. Sometimes a user may add blank lines intentionally. How can I detect these lines and delete them? I'm using PHP. I dont mind using a regexp or anything else.

Incorrect Data

Matthew
Mark
Luke

John

James

Correct Data (Note blank lines removed)

Matthew
Mark
Luke
John
James
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
487 views
Welcome To Ask or Share your Answers For Others

1 Answer

Using regex to eliminate blank lines before exploding (works well for any number of consecutive blank lines, also see next snippet):

$text = preg_replace('/
+/', "
", trim($_POST['textarea']));

Splitting with a regex:

$lines = preg_split('/
+/', trim($_POST['textarea']));
$text = implode("
", $lines);

Splitting without a regex:

$lines = array_filter(explode("
", trim($_POST['textarea'])));
$text = implode("
", $lines);

Just feeling a tad creative today, pick your poison :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...