tags:

views:

40

answers:

2

Hey,

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', '', $message, 1);
}

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

+1  A: 

a) better not use regex...

b) use recursion.

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

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

nikic
Ah, I forgot that PCRE can do recursive matching.
Gumbo
Could you please give me an short example of a recursive code? Because I think this is not something that I learn in a couple of days, isn't it?
Arjen
I edited my post to include an example ;)
nikic
A: 

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]
kiamlaluno
That's exactly right.
Arjen