bbcode little problem

Discussion in 'PHP' started by burhankhan, Jul 8, 2007.

  1. #1
    Hi:

    I have written a class to parse bbcode.

    I am having little problem with regular expression.

    preg_replace('/\[quote\](.+?)\[\/quote\]/is', '<pre>$1</pre>', $mytext)
    Code (markup):
    This is simple code to convert all QUOTES tags. It is working fine.
    But when i have text like this:

    [ quote ]here is some text[ quote ]here is more text[ / quote ][ / quote ]
    Code (markup):
    Then it is creating problem.
    It will print this:

    [ quote ]here is some text
    
    [ / quote ]
    Code (markup):
    Is there any solution to parse 2 or more level QUOTES??

    Thanks
    Burhan Khan
     
    burhankhan, Jul 8, 2007 IP
  2. daman371

    daman371 Peon

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this.
    preg_replace('@\[(?i)quote\](.*?)\[/(?i)quote\]@si', '<pre>$1</pre>', $mytext)
    Code (markup):
     
    daman371, Jul 8, 2007 IP
  3. burhankhan

    burhankhan Well-Known Member

    Messages:
    489
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Still not working.......
    Same output as previous.
     
    burhankhan, Jul 8, 2007 IP
  4. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #4
    
    /\[\s+?quote\s+?\](.+?)\[\s+?\/\s+?quote\s+?\]/si
    
    Code (markup):
     
    ansi, Jul 8, 2007 IP
  5. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Judging by the previous replies, I think the spaces were just to stop the forum parsing the quote tags. The problem is not with the regex, it's the nested bbcode - without a much more complex parser, you can't match up the corresponding open and close tags.

    After parsing a quote tag, it continues from the end position of the last replacement to look for the next opening quote tag. Obviously that won't work if we have a nested quote tag because we need it to go back to the beginning in order to find it.

    As already mentioned, you would need a more advanced parser to match up the open quote tags with their respective close tags. However, if we just put the replacement in a loop, it will continue finding pairs of tags till everything has been converted.

    I dunno if that makes sense, hopefully should do with an example. (I'll rename quote to "tag" so I can use the [tag] syntax without the forum bbcode parser interfering).

    Using the example text in the OP, the first replacement would convert:
    [b][tag]here is some text[tag]here is more text[/tag][/b][/tag]
    Code (markup):
    Now if we rerun the replacement, it will next work on:
    <tag>here is some text[b][tag]here is more text</tag>[/tag][/b]
    Code (markup):
    And that gets converted to our desired format:
    <tag>here is some text<tag>here is more text</tag></tag>
    Code (markup):
    The parser never actually works out how much it should consume to match up the pairs of [tag] and [/tag] correctly, but by continuing the replacements till all pairs are converted (albeit mismatched pairs), we can get the desired end result.
    while ( preg_match('/\[quote\](.+?)\[\/quote\]/is', $mytext) ) 
        $mytext = preg_replace('/\[quote\](.+?)\[\/quote\]/is', '<pre>$1</pre>', $mytext);
    PHP:
     
    rodney88, Jul 9, 2007 IP
  6. burhankhan

    burhankhan Well-Known Member

    Messages:
    489
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    108
    #6
    Thanks rodney88 , it is working for me now!

    But i have little more problem.

    Actually i want to remove sub string from a string.

    Criteria is like this:

    this is starting of string [B][tag][tag] here is some other code[/tag]here is some code[/tag][/B] this is ending of string...
    Code (markup):
    You can see in this string there are many nested [tags].

    I just want to remove those all nested tags. And out put should be:

    this is starting of string  this is ending of string...
    Code (markup):
    I have tried many patterns but useless.
    Any help will be highly appreciated.

    Thanks
    Burhan Khan
     
    burhankhan, Jul 9, 2007 IP