微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用php正则表达式替换方括号之间的子字符串

这是我正在使用的子字符串

[sitetree_link%20id=2]

我需要用空格替换[]之间所有出现的事件.但显然如果有[]括号之外,请不要管它们……

我现在正在学习正则表达式,但这个看起来很难.有人为此获得了超级聪明的正则表达式吗?

谢谢 :)

解决方法:

你可以试试这个

$result = preg_replace('/(\[[^]]*?)(%20)([^]]*?\])/m', '$1 $3', $subject);

说明

(          # Match the regular expression below and capture its match into backreference number 1
   \[         # Match the character “[” literally
   [^]]       # Match any character that is NOT a “]”
      *?         # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
)
(          # Match the regular expression below and capture its match into backreference number 2
   %20        # Match the characters “%20” literally
)
(          # Match the regular expression below and capture its match into backreference number 3
   [^]]       # Match any character that is NOT a “]”
      *?         # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
   \]         # Match the character “]” literally
)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐