我需要在 PHP 中使用 preg_replace 仅替换括号中最后出现的任何内容
我需要将字符串末尾括号中出现的所有内容替换为空。但我不希望同样的事情发生在字符串内的任何东西上。例如,如果我有一个字符串“特殊(非)值(SV)”,那么 preg_replace 应该只替换末尾括号,因此结果应该是“特殊(非)值”
I need to replace all the occurrence of anything in parenthesis at the end of the string with nothing. But i dont want same to happen to anything that is inside the string. For example if i have a string "Special (NOT) Value (SV)" then the preg_replace should only replace the end parenthesis so the result should be "Special (NOT) Value"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尝试过使用爆炸功能吗?您可以定义模式,然后可以连接数组... http://php .net/manual/en/function.explode.php
Have you tryed using the explode function? You can define the pattern, and you can concatenate the arrays afterwards... http://php.net/manual/en/function.explode.php
如果您查看手册,您会发现可以使用
$
字符来断言行尾。因此blah.*?$
将查找字符串中最后一次出现的blah
。这不是完整的答案,但应该足以让您继续下去。
If you look at the manual, you'll see that you can use the
$
character to assert the end of a line. Soblah.*?$
will find the last occurrence ofblah
in a string.That's not the complete answer, but should be enough to get you going.
美元字符匹配字符串结尾。下面的正则表达式应该执行您想要的操作,除非您可以在括号内包含括号,在这种情况下,您应该更准确地定义您期望得到的答案。 (现在我会让你了解发生了什么以及你的第一次尝试出了什么问题)
the dollar character match end of string. The regex below should do what you want, except if you can have parenthesis inside parenthesis, in wich case you should define more precisely what you'are expecting to get an answer. (Now I'll leave you understand what's going on and what is wrong with your first tries)