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

BBCode

Daniel Hood

Well-known member
#1
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...
 

Jeremy

Well-known member
#2
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. :)
 

Daniel Hood

Well-known member
#3
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?
 

Jeremy

Well-known member
#4
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);
 

Jeremy

Well-known member
#7
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.
 

SheepCow

Well-known member
#8
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