Help with quotes -- removing/replacing

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Help with quotes -- removing/replacing

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

bumpity bump bump.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Re: Help with quotes -- removing/replacing

Post 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.
 
?>
Post Reply