Removing nested bbcode (quotes) in PHP

jeffknight

New Member
I'm trying to remove nested quoting from my bulletin board, but I'm having some issues.Example input:\[quote\] [quote author=personX link=topic=12.msg1910#msg1910 date=1282745641]\[code\][quote author=PersonY link=topic=12.msg1795#msg1795 date=1282727068]The message in the original quote[/quote]\[/code\] A second message quoting the first one [/quote] [quote author=PersonZ link=topic=1.msg1#msg1 date=1282533805] A random third quote [/quote]\[/quote\]Example output\[quote\] [quote author=personX link=topic=12.msg1910#msg1910 date=1282745641] Message in the second quote [/quote] [quote author=PersonZ link=topic=1.msg1#msg1 date=1282533805] A random third quote [/quote]\[/quote\]As you can see the nested quote (The original message) is removed, along with the quote tags.I can't seem to figure it out.When i try\[code\]$toRemove = '(\\[)(quote)(.*?)(\\])';$string = $txt;$found = 0; echo preg_replace("/($toRemove)/e", '$found++ ? \'\' : \'$1\'', $string);\[/code\]It removes every occurrence of of the quote tag except the first one,But when i expand the code to:\[code\]$toRemove = '(\\[)(quote)(.*?)(\\])(.*?)(\\[\\/quote\\])';$string = $txt;$found = 0; echo preg_replace("/($toRemove)/e", '$found++ ? \'\' : \'$1\'', $string); \[/code\]It stops doing anything at all.Any ideas on this ?Edit:Thanks for your help, Haggi.Ik keep running in to trouble though.The while loop around \[code\]while ( $input = preg_replace_callback( '~\[quoute.*?\[/quote\]~i', 'replace_callback', $input ) ) {// replace every occurence}\[/code\]causes the page to loop indefinitely, when removed (along with the extra u in quoute), the page doesn't do anything.I've determined that the cause is the matchingwhen changed to\[code\]$input = preg_replace_callback( '/\[quote(.*?)/i', 'replace_callback', $input );\[/code\]the code does start working, but when changed to\[code\]$input = preg_replace_callback( '/\[quote(.*?)\[\/quote\]/i', 'replace_callback', $input );\[/code\]It stopts doing anything again.Also, there is an issue with the undo_replace function as it never finds the stored hash, it only gives warnings about unfound indexes. The regex matching the sha1 isn't working correctly i guess.The complete code as I have it now:\[code\]$cache = array();$input = $txt;function replace_callback( $matches ) { global $cache; $hash = sha1( $matches[0] ); $cache["hash"] = $matches[0]; return "REPLACE:$hash";}// replace all quotes with placeholders$input = preg_replace_callback( '/\[quote(.*?)\[quote\]/i', 'replace_callback', $input );function undo_replace( $matches ) { global $cache; return $cache[$matches[1]];}// restore the outer most quotes$input = preg_replace_callback( '~REPLACE:[a-f0-9]{40}~i', 'undo_replace', $input );// remove the references to the inner quotes$input = preg_replace( '~REPLACE:[a-f0-9]{40}~i', '', $input );echo $input;\[/code\]Thanks again for any ideas guys :)
 
Back
Top