php 删除多个空格
我遇到了一些麻烦,我想知道是否有人知道 preg_replace 正则表达式,它可以删除除字符串中遇到的第一个空格之外的所有空格。
|示例|
我有以下字符串:“My First Last Name”
我想要实现的目标是:“My FirstLastName”
抱歉,但我对正则表达式非常不好:( 所以非常感谢任何帮助。
I've been having some trouble, i was wondering if anyone knows of a preg_replace regex which can remove all spaces except the first one it encounters from a string.
|EXAMPLE|
I have the following string: "My First Last Name"
What i would like to achieve is something like: "My FirstLastName"
Sorry but i'm pretty bad with regex :( so any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您并不需要正则表达式来执行此操作,只需将字符串拆分为空格,然后再次将其连接起来会更快。
You don't actually need regex to do that, it's quicker to just split the string on spaces and then join it up again.
不需要使用正则表达式,只需找到第一个空格,保留该字符串的那一部分,然后替换其余部分:
No need to use regex, just find the first space, keep that piece of the string, and replace the rest: