从文本区域的输出中删除空行
我从文本区域获取数据,用户必须在每一行上输入一个名称。该数据随后在回车符处被分割。有时用户可能会故意添加空行。我怎样才能检测到这些行并删除它们?我正在使用 PHP。我不介意使用正则表达式或其他任何东西。
不正确的数据
Matthew
Mark
Luke
John
James
正确的数据(注意删除空行)
Matthew
Mark
Luke
John
James
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用正则表达式消除爆炸之前的空行(适用于任意数量的连续空行,另请参阅下一个片段):
使用正则表达式进行拆分:
不使用正则表达式进行拆分:
今天感觉有点创意,选择你的毒药:)
Using regex to eliminate blank lines before exploding (works well for any number of consecutive blank lines, also see next snippet):
Splitting with a regex:
Splitting without a regex:
Just feeling a tad creative today, pick your poison :)
我相信,简单的字符串替换应该可以解决问题。
A simple string replace should do the trick, I believe.
分割输入后,循环数组搜索空行:
您甚至可以查找只包含空格的行来删除它们:(
两个代码片段都未经测试)
注意:按换行符分割时要小心(我用 \n 分割它们) ,但如果客户端浏览器在 Windows 上运行,则可能为 \r\n)。
After splitting your input, loop the array searching for empty lines:
You could even look for lines containing just spaces to also delete them:
(Both code snippets are untested)
NOTE: Be careful when splitting by line breaks (I splitted them by \n, but could be \r\n if the client browser runs on Windows).