Regex nested forum quotes (BBCode)

DavidQX

New Member
I'm having trouble with regex, I'm an absolute regex noob. I can't see what's going wrong with trying to convert the HTML back to the 'BBCode'.Could somebody take a look at the 'unquote' function and tell me the obvious mistake I'm making? (I know it's obvious because I always find the un-obvious errors)NOTE: I'm not using recursive Regex because I couldn't get my head around it and already started this way round of sorting out the Quotes so they're nested.\[code\]<?phpfunction quote($str){ $str = preg_replace('@\[(?i)quote=(.*?)\](.*?)@si', '<div class="quote"><div class="quote-title">\\1 wrote:</div><div class="quote-inner">\\2', $str); $str = preg_replace('@\[/(?i)quote\]@si', '</div></div>', $str); return $str;}function unquote($str){ $str = preg_replace('@\<(?i)div class="quote"\>\<(?i)div class="quote_title"\>(.*?)wrote:\</(?i)div\><(?i)div class="quote-inner"\>(.*?)@si', '
\\1 said:
\\2', $str); $str = preg_replace('@\</(?i)div\></(?i)div\>@si', '
', $str);}?>\[/code\]This is just some code to help test it:\[code\]<html><head> <style> body { font-family: sans-serif; } .quote { background: rgba(51,153,204,0.4) url(../img/diag_1px.png); border: 1px solid rgba(116,116,116,0.36); padding: 5px; } .quote-title, .quote_title { font-size: 18px; margin: 5px; } .quote-inner { margin: 10px; } </style></head><body> <?php $quote_text = '
VCMG said:
2xAA said:
DO RECURSIVE QUOTES WORK?
I have no idea.
'; $quoted = quote($quote_text); echo $quoted.'<br><br>'.unquote($quoted); ?></body>\[/code\]Thanks in advance, Sam.
 
Back
Top