BBCode

Daniel Hood

Well-known member
What is the appropriate way to send a variable through the bbcode parser? For example something like:
Code:
$message = XenForo_BBCode->parse($message);
I'm not sure what it is...
 
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. :)
 
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?
 
Yes. You would want to create the parser using the new constructor. You would use this:

PHP:
$formatter = new XenForo_BbCode_Formatter_Base;
$parser = new XenForo_BbCode_Parser($formatter);
$parser->parse($message);
 
That works but now it returns an array. I'm looking for it to return the message with the bbcode changed to html.
 
Actually, my create statement was wrong (shows how long my original code read was). It should be this:

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

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

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

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

IIRC
 
Top Bottom