删除除最后一个逗号之外的逗号 w preg_replace
我的头在旋转,我尝试自己做这件事,但无法弄清楚。所以我将再次求助于你们的知识。
这些都是我可能的字符串:(
My head is spinning with, pregreplace
My head is spinning, with, pregreplace
My head is, spinning, with, pregreplace
My head, is, spinning, with, pregreplace
注意上面字符串中的逗号)
我希望所有“preg-replaced”/“string-replaced”末尾只有一个逗号。(就像第一个示例中显示的那样)
我的头是旋转,pregreplace
提前致谢;)
My head is spinning, I tried to do this on my own, but cant figure it out. so once again I will turn to your guys knowledge.
These all are my possible my strings:
My head is spinning with, pregreplace
My head is spinning, with, pregreplace
My head is, spinning, with, pregreplace
My head, is, spinning, with, pregreplace
(Notice commas in above strings)
I want to have all "preg-replaced" / "string-replaced" with only one comma at the end.(Just like displayed on first example)
My head is spinning with, pregreplace
Thanks in advance ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用“正向前瞻”,如下所示:
,(?=.*,)
前瞻是括号中的部分。它基本上是说“只有在字符串后面还有另一个逗号时才替换这个逗号。
代码看起来像这样:
我用 RegexBuddy 测试了它以确认它有效:
You can use a "positive lookahead" like this:
,(?=.*,)
The lookahead is the part in parens. It basically says "only replace this comma if there's another comma later in the string.
The code would look like this:
I tested this with RegexBuddy to confirm it works:
上面应该可以满足您的需要。
The above should do what you need.