php preg_split 最后一次出现的字符
寻求帮助!
我需要在最后一次出现空格时拆分字符串...
例如“Great Neck NY”我需要拆分它,这样我就有“Great Neck”和“NY”
我在使用 preg_split 和基本内容时没有遇到问题但我很难弄清楚如何告诉它只在最后一次出现时分裂!任何帮助将不胜感激!
麦克风
Looking for some help!
I need to split a string at the last occurrence of a space...
e.g. "Great Neck NY" I need to split it so I have "Great Neck" and "NY"
I haven't had a problem using preg_split with basic stuff but I'm stumped trying to figure out how to tell it only to split at the last occurrence! Any help would be appreciated!
Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 lookahead 断言:
现在字符串将在
处拆分仅当
(空白字符)。(?=\S+$)
从此时开始匹配时, \s+\S+$
匹配字符串末尾紧邻的非空白字符。You could use a lookahead assertion:
Now the string will be split at
\s+
(whitespace characters) only if(?=\S+$)
would match from this point on. And\S+$
matches non-whitespace characters immediately at the end of the string.