\HTML_BBCodeParser2
- Author: Stijn de Reede <sjr@gmx.co.uk> This is a parser to replace UBB style tags with their html equivalents. It does not simply do some regex calls, but is complete stack based parse engine. This ensures that all tags are properly nested, if not, extra tags are added to maintain the nesting. This parser should only produce xhtml 1.0 compliant code. All tags are validated and so are all their attributes. It should be easy to extend this parser with your own tags, see the _definedTags format description below. Usage: $parser = new HTML_BBCodeParser2($options = array(...)); $parser->setText('normal [b]bold[/b] and normal again'); $parser->parse(); echo $parser->getParsed(); or: $parser = new HTML_BBCodeParser2($options = array(...)); echo $parser->qparse('normal [b]bold[/b] and normal again'); or: echo HTML_BBCodeParser2::staticQparse('normal [b]bold[/b] and normal again'); Setting the options from the ini file: $config = parse_ini_file('BBCodeParser.ini', true); $options = $config['HTML_BBCodeParser2']; The _definedTags variables should be in this format: array('tag' // the actual tag used => array('htmlopen' => 'open', // the opening tag in html 'htmlclose' => 'close', // the closing tag in html, can be set to an empty string if no closing tag is present in html (like <img>) 'allowed' => 'allow', // tags that are allowed inside this tag. Values can be all or none, or either of these two, followed by a ^ and then followed by a comma seperated list of exceptions on this 'attributes' => array() // an associative array containing the tag attributes and their printf() html equivalents, to which the first argument is the value, and the second is the quote. Default would be something like this: 'attr' => 'attr=%2$s%1$s%2$s' ), 'etc' => (...) )
Synopsis
class HTML_BBCodeParser2
{
- // members
- public array $_definedTags = ;
- public string $_text = '';
- public string $_preparsed = '';
- public array $_tagArray = ;
- public string $_parsed = '';
- public array $_options = ;
- public array $_filters = ;
- // methods
- public none __construct()
- public void setOption()
- public void addFilter()
- public void removeFilter()
- public boolean addFilters()
- public none _preparse()
- public none _buildTagArray()
- public array _buildTag()
- public none _validateTagArray()
- public boolean _parentNeeded()
- public boolean _childNeeded()
- public boolean _isAllowed()
- public none _buildParsedString()
- public none setText()
- public string getText()
- public string getPreparsed()
- public string getParsed()
- public none parse()
- public none qparse()
- public none staticQparse()
Members
public
- $_definedTags
—
array
An array of tags parsed by the engine, should be overwritten by filters - $_filters
—
array
An array of filters used for parsing - $_options
—
array
An array of options, filled by an ini file or through the contructor - $_parsed
—
string
A string containing the parsed version of the text - $_preparsed
—
string
A string containing the preparsed input - $_tagArray
—
array
An array tags and texts build from the input text - $_text
—
string
A string containing the input
Methods
public
- __construct() — Constructor, initialises the options and filters
- _buildParsedString() — Builds a parsed string based on the tag array
- _buildTag() — Builds a tag from the input string
- _buildTagArray() — Builds the tag array from the input string $_text
- _childNeeded() — Checks to see if a child is needed
- _isAllowed() — Checks to see if a tag is allowed inside another tag
- _parentNeeded() — Checks to see if a parent is needed
- _preparse() — Executes statements before the actual array building starts
- _validateTagArray() — Validates the tag array, regarding the allowed tags
- addFilter() — Add a new filter
- addFilters() — Add new filters
- getParsed() — Gets the parsed text from the object
- getPreparsed() — Gets the preparsed text from the object
- getText() — Gets the unparsed text from the object
- parse() — Parses the text set in the object
- qparse() — Quick method to do setText(), parse() and getParsed at once
- removeFilter() — Remove an existing filter
- setOption() — Option setter
- setText() — Sets text in the object to be parsed
- staticQparse() — Quick static method to do setText(), parse() and getParsed at once