newbtophp Posted April 11, 2010 Share Posted April 11, 2010 Ok I've coded a simple BBCode parser using regex. I got the [ quote ] bbcode to work recursively so it can allow quote bbcode within each others, however I want to also support the following [ quote name=newbtophp ] bbcode, I used the same method as the [ quote ] but doesnt seem to work anyone can help function Quote($name='null',$text) { $regex = '~\[quote\](.*)\[/quote\]~ise'; $regex2 = '~\[quote=(.*)\](.*)\[/quote\]~ise'; if($name != "null"){ $text = "<fieldset class=\"quote\"><legend>Quote - {$name}</legend>{$text}</fieldset>"; } else { $text = "<fieldset class=\"quote\"><legend>Quote</legend>{$text}</fieldset>"; } $text = preg_replace($regex, 'Quote(\'null\',\'$1\')', $text); $text = preg_replace($regex2, 'Quote(\'$1\',\'$2\')', $text); return $text; } function BBcode($Text){ $Text = preg_replace('~\[quote\](.*)\[/quote\]~ise', 'Quote(\'null\',\'$1\')', $Text); $Text = preg_replace('~\[quote=(.*)\](.*)\[/quote\]~ise', 'Quote(\'$1\',\'$2\')', $Text); return $Text; } Example scenario: <?php echo BBcode('[quote] [quote name=testuser] b u m p[/quote] [/quote]'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/198220-preg_replace-bbcode-quote-issue/ Share on other sites More sharing options...
newbtophp Posted April 11, 2010 Author Share Posted April 11, 2010 Figured a workaround. Quote Link to comment https://forums.phpfreaks.com/topic/198220-preg_replace-bbcode-quote-issue/#findComment-1040060 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.