开发者

Delete everything between "quote" tags ([quote])

开发者 https://www.devze.com 2023-01-10 19:50 出处:网络
I have nested quotes enabled in my private message system, but now I want to show the user a part of the message when they hover the title of the message.

I have nested quotes enabled in my private message system, but now I want to show the user a part of the message when they hover the title of the message.

To get the relevant text out of the message I want to delete the quote parts.

The quote structure is as follows:

[quote]
  [quote]
    dasdasa adsadsa ds a
  [/quote]

  ddasd asd ads adsasd
[/quote]

How can I remove everything between de quote tags?

I have tried it with the following code, but the last part of the nested quotes won't remove:

while(preg_match('#\[quote=(.*?)\](.*?)\[\/quote\]#si', $message)) {
  $message = preg_replace('#\[quote=(.*?)\](.*?)\[\/quote\]#si', '', $message, 1);
}

while(preg_match('#\[quote\](.*?)\[\/quote\]#si', $message)) {
  $message = preg_replace('#\[quote\](.*?)\[\/quote\]#si', '', $mess开发者_运维问答age, 1);
}

Any suggestions how to delete these quotes out of the message? Thx!


a) better not use regex...

b) use recursion.

#\[quote](?:(?>[^[])|\[(?!/?quote])|(?R))*\[/quote]#

I haven't tested the pcre, but it should do.


There is no need to call preg_match(), and then call preg_replace(), when preg_replace() can replaces all strings matching the regular expression with a single call.

The problem with the regular expression you are using is that the code you reported would produce the following output:

  ddasd asd ads adsasd
[/quote]
0

精彩评论

暂无评论...
验证码 换一张
取 消