1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.


Discussion in 'XenForo Development Discussions' started by Daniel Hood, May 17, 2011.

  1. Daniel Hood

    Daniel Hood Well-Known Member

    What is the appropriate way to send a variable through the bbcode parser? For example something like:
    $message = XenForo_BBCode->parse($message);
    I'm not sure what it is...
  2. Jeremy

    Jeremy Well-Known Member

    I haven't delved into the parser, but I can give a rough estimation based on what I just read through. The variable passed into $XenForo_BBCode->parse() (as a side note, parse isn't static, so the parser must be a variable) is what you want to run through. For instance, any BBCode I type into this message is placed within $message inside most of the code. It sends the entire unparsed string into the parser. The parse steps through and finds any tags and replaces them accordingly. Once it reaches the end, it returns a string without any valid BBCode present. It has all be turned into the corresponding HTML replacements. Most XenForo code (if I have read correctly, which I haven't done in a while) assigns it back to $message, to allow for easy remembrance later in the code. So the code process is thus:
    1. $message = "some [b]bold[/b] text"
    2. $message = $parser->parse($message)
    3. // $message = "some <strong>bold</strong> text"
    Hopefully my 3:20 am code explanation isn't confusing. :)
    EQnoble likes this.
  3. Daniel Hood

    Daniel Hood Well-Known Member

    Thank you,
    I understand how it works I'm more confused on how to access the parse function. would it be $parser = new XenForo_BBCode; $message = $parser->parse($message); ? or is there already an established class that I can use?
  4. Jeremy

    Jeremy Well-Known Member

    Yes. You would want to create the parser using the new constructor. You would use this:

    $formatter = new XenForo_BbCode_Formatter_Base;
    $parser = new XenForo_BbCode_Parser($formatter);
  5. Daniel Hood

    Daniel Hood Well-Known Member

    That works but now it returns an array. I'm looking for it to return the message with the bbcode changed to html.
  6. Daniel Hood

    Daniel Hood Well-Known Member

    Thanks for the help by the way.
  7. Jeremy

    Jeremy Well-Known Member

    Actually, my create statement was wrong (shows how long my original code read was). It should be this:

    $bbCodeParser = new XenForo_BbCode_Parser(XenForo_BbCode_Formatter_Base::create('Base'));
    Running parse() should work with that.
  8. SheepCow

    SheepCow Well-Known Member

    parse() parses it into a tag tree, render() will generate the HTML for you, .e.g

    $bbCodeParser = new XenForo_BbCode_Parser(XenForo_BbCode_Formatter_Base::create('Base'));

    $bbcode '[b]hello[/b]';
    $html $bbCodeParser->render($bbcode);

Share This Page