Page 1 of 1

Help with quotes -- removing/replacing

Posted: Wed Oct 04, 2006 10:03 pm
by s.dot
So, i'm going to be annoying here for a sec to show what i mean... best way to describe it.
1
2
3
See, when there's like 12 of those, it gets really annoying and breaks layouts.. as recently made in a suggestion on my forums. read it here

What I want to do is drop all quotes EXCEPT the most recent three. So, it would be something like [ quote ] [ quote ] [ quote ] something ((((((((all other quotes in here get stripped out since this is the third innermost quote)))))))) [ /quote ] something else [ /quote ] more something [ /quote]

Now, if i were only wanting the last quote, I think I could do that. But I'm no regex guru! Can someone give me a pointer?

Posted: Thu Oct 05, 2006 10:05 pm
by s.dot
bumpity bump bump.

Re: Help with quotes -- removing/replacing

Posted: Wed Oct 11, 2006 8:32 am
by bokehman
Sorry if the expression breaks this page! I haven't tested this except on that particular string. It relies on greediness so would cause problems if there were more than one nested quote each with a level greater than 4. This would mean the section of text between the double nesting might be lost. Nevertheless that would be a very unusual situation. I'm still thinking about how to avoid that! I guess the easiest way to avoid it would to build a stack, but that's not regex and this is the regex forum.

Code: Select all

<?php

$target = 'Outside the quotes [quote="scottayy"]So, i\'m going to be [quote]1[quote]2[quote]3[quote]4[quote]5[/quote][/quote]ww[/quote][/quote][/quote] See, when there\'s like 12 of those, it gets really annoying and breaks layouts...  [url=http://www.showmypro.com/forums/view-topic/22169/quotes.php]read it here[/url] Can someone give me a pointer?[/quote] outside the quotes.';

$expression = '/((?:\[quote[^\]]*\](?:(?!\[\/?quote[^\]]*\]).)*){3})\[quote[^\]]*\].*\[\/quote\]((?:(?:(?!\[\/?quote[^\]]*\]).)*\[\/quote\]){3})/is';

echo preg_replace($expression, '$1$2', $target);

#prints...

# Outside the quotes [quote="scottayy"]So, i'm going to be [quote]1[quote]2[/quote][/quote] See, when there's like 12 of those, it gets really annoying and breaks layouts... [url=http://www.showmypro.com/forums/view-topic/22169/quotes.php]read it here[/url] Can someone give me a pointer?[/quote] outside the quotes.
 
?>