如何删除空白区域后面的所有内容?
如何删除空白区域后面的所有内容。我有一个日期,例如: 10.10.2010 18:34,年份和 18 之间有一个空格。我只需要字符串的第一部分(仅 10.10.2010)。所以我尝试使用 preg_replace 删除空白区域后面的所有内容,但它不起作用。我的表情应该是怎样的?
感谢您的帮助! phpheini
how can I delete everything that is behind an empty space. I have a date like: 10.10.2010 18:34 with an empty space between the year and the 18. I only need the first part of the string (only 10.10.2010). So I tried to use preg_replace to remove everything behind the empty space, but it doesnt work. How would my expression have to be?
Thank you for your help!
phpheini
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的简单要求,您可以简单地使用 strtok()
编辑: 我认为涉及正则表达式的唯一原因是在提取部分的同时验证日期时间字符串。例如
Based on your simple requirements, rather than going with a full regex solution, you can simply tokenize the string using strtok()
Edit: The only reason I could see to involve regular expressions would be to validate the date-time string at the same time as extracting parts. For example
如果您对格式有信心:
If you're confident of the formatting:
键盘。
然而,正如 Phil Brown 指出,使用像
strtok()
这样的东西对于像这样的简单任务来说要好得多(比较答案,很明显)。CodePad.
As Phil Brown states, however, using something like
strtok()
is much better for a simple task like this (compare the answers and it is obvious).