Results 1 to 1 of 1

Thread: BBCode Parsing Error

  1. #1
    Join Date
    Feb 2010
    Thanked 0 Times in 0 Posts

    Question BBCode Parsing Error

    This problem has been resolved!


    I've been working with Regular Expressiosn for a long time, so feel free to use special words that normal programmers would otherwise have no knowledge of

    I've been making a BBCode parser and it has been going well, exept for one issue. This is using two of the same tags in the same piece of text.

    Apparently the preg_replace() function will match the longest string, so if you have two tags, it will start the formatting at the first occurance of the opening tag, then end it at the last occurance of the closing tag. Like such:

    (I'll use braces intead of brackets so this forum does not parse the BBCode I use as an example)

    This is some {B}bold{/B} text. This is more {B}bold{/B} text.

    The above is wrongly parsed as:

    This is some bold{/B} text. This is more {B}bold text.

    It should be parsed as:

    This is some bold text. This is more bold text.

    Here's the line of code that parses that tag:

    $text = preg_replace("/\[b\](.+)\[\/b\]/is", "<b>$1</b>", $text);
    Please take note that I did use the plus sign ("+") operator that matches 0 or more occurances of the previous character "nongreedily", meaning it will match the fewest amount of occurances. This is opposed to the asterisk ("*") operator that matches 0 or more occurances of the previous character (like the plus operator), but the asterisk does so "greedily", matching as many as possible.



    $text = preg_replace("/\[b\](.+?)\[\/b\]/is", "<b>$1</b>", $text);
    The question mark in (.+?)!
    Last edited by Ozzypig; 02-20-2010 at 11:45 PM. Reason: RESOLVED! =D


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts