- Messages
- 5,324
I tried both of these and none of them seemed to work. Always just quoted the post and cut all the other quotes out as it did before.bluegate said:Regex is always such fun!
I can't claim much credit for this as I found most of it on the internet, but here are two possible solutions;
#1 Changing the Regex to
For more information see the accepted answer on this page;Code:// Remove any nested quotes, if necessary. if (!empty($modSettings['removeNestedQuotes'])) $form_message = preg_replace('~\G(?!\A)(?>(\)|(?<!\[)(?>[^[]+|\[(?!/?quote))+\K)|\[quote\b[^]]*]\K~', '', $form_message);
https://stackoverflow.com/questions/18754062/remove-nested-quotes/18754616
#2 Creating a Parser function which can be told to easily remove quotes up to a set level
The 2 at the end on the very last line is the nesting level for quotes.Code:// Function to remove nested BB Code starting at a certain level function removeNestedBBCode( $text , $pattern , $level ) { preg_match_all( $pattern , $text, $matches, PREG_OFFSET_CAPTURE); $nestlevel = 0; $cutfrom = 0; $cut = false; $removed = 0; foreach($matches[0] as $quote){ if (substr($quote[0], 0, 2) == '[q') $nestlevel++; if (substr($quote[0], 0, 2) == '[/') $nestlevel--; if (!$cut && $nestlevel == $level){ // we reached the first nested quote, start remove here $cut = true; $cutfrom = $quote[1]; } if ($cut && $nestlevel == ($level - 1 ) ){ // we closed the nested quote, stop remove here $cut = false; $text = substr_replace( $text , '' , $cutfrom - $removed , ($quote[1]+8) - $cutfrom ); $removed += ($quote[1]+8) - $cutfrom; } }; return $text; } // Remove any nested quotes, if necessary. if (!empty($modSettings['removeNestedQuotes'])) $form_message = removeNestedBBCode( $form_message, '#(\[quote[^]]*\]|\[\/quote\])#' , 2 );
This is based on the third answer found on this page, although it took some editing to actually get it to work;
https://stackoverflow.com/questions/18754062/remove-nested-quotes/18754616
If you want to play around with both of these solutions then you can go to https://dev.luckymouse.nl/testMatch.php and play around with them, this could be helpful if you want to determine the nesting level to use, if you were to pick option #2.
You'll always end up with some degree of a mess where quotes are removed from a post but the text surrounding them stays, though.
Maybe that's not where the actual quote-cutting code is...