Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

parse BB-Code using Regex/PHP

Last post 10-06-2008, 3:06 PM by ddrudik. 4 replies.
Sort Posts: Previous Next
  •  10-06-2008, 10:34 AM 46911

    parse BB-Code using Regex/PHP

    hello,
    i've written a php-bb-parser that uses the 'preg_replace_callback' function.
    bb-code looks like this:

    [tag=opt. attrib]value[/tag]
    for example:

    bold , or label

     
    my regex looks like this:

    #\[(.+)=?(.*?)?\](.*?)\[\/\1\]#

    the callback function will then process the backreferences and create html. the function is called until the string I have before 'preg_replace_callback' is similar to the one after I called the function, so that it will process multiple/nested tags correctly.

    it does work fine for the two examples I gave above. It also works for nested tags like bold+italic
    now I'm trying to implement an (un)ordered list; the syntax shall look as following:

    [ul]
    Lightningitem0[/li]
    Lightningitem1[/li]
    Lightning...[/li]
    [/ul]

    the problem is that it will parse the li-tags correctly but leave out the ul-tag.
    I've already tried the posix mods 's', 'x', 'm'

     

    I'm looking forward to your replies, thank you in advance.

     

    edith says: this board uses BB-Code too, what a mess. 

     



     

     

  •  10-06-2008, 10:48 AM 46912 in reply to 46911

    Re: parse BB-Code using Regex/PHP

    abc-regex:

    ...

    edith says: this board uses BB-Code too, what a mess. 

    Yes, perhaps you could post the code/regex at pastbin or something similar?

    http://pastebin.com/

  •  10-06-2008, 10:52 AM 46913 in reply to 46911

    Re: parse BB-Code using Regex/PHP

    You might want to show the relevant PHP source code as well as the example text.


    looking for a new regex book?
    Regular Expressions Cookbook
  •  10-06-2008, 11:27 AM 46915 in reply to 46913

    Re: parse BB-Code using Regex/PHP

    the php: http://pastebin.com/m1fa13ee7

    and an example-haystack:

    [ol]

    [li]list item[/li]

    [li]another list item[/li]
    [/ol]

     

  •  10-06-2008, 3:06 PM 46925 in reply to 46915

    Re: parse BB-Code using Regex/PHP

    This seemed to work for me:

    http://pastebin.com/f6ed766c1

    This version handles unknown "[tag]...[/tag]" blocks by prepending them with "UNKNOWN:".

    You might consider a pre-made solution for your project:

    http://pear.php.net/package/HTML_BBCodeParser




    looking for a new regex book?
    Regular Expressions Cookbook
View as RSS news feed in XML