BBCodeParser
extends Parser
in package
Parses Bulletin Board Code in a string and converts it to HTML.
Table of Contents
Constants
- INPUT_BBC = 0b1
- INPUT_MARKDOWN = 0b10
- INPUT_SMILEYS = 0b100
- OUTPUT_BBC = 2
- OUTPUT_HTML = 0
- OUTPUT_TEXT = 1
Properties
- $custom_smileys_enabled : bool
- $defalt_options : array<string|int, mixed>
- $disabled : array<string|int, mixed>
- $enable_bbc : bool
- $enable_markdown : bool
- $enable_post_html : bool
- $encoding : string
- $for_print : bool
- $locale : string
- $parse_tags : array<string|int, mixed>
- $smiley_set : string
- $smileys_url : string
- $time_format : string
- $time_offset : int
- $alltags_regex : string|null
- $codes : array<string|int, mixed>
- $itemcodes : array<string|int, mixed>
- $smileys : bool
- $bbc_codes : array<string|int, mixed>
- $bbc_lang_locales : array<string|int, mixed>
- $hosturl : string
- $inside : array<string|int, mixed>|null
- $integrate_bbc_codes_done : bool
- $last_pos : int
- $message : string
- $open_tags : array<string|int, mixed>
- $parsers : array<string|int, mixed>
- $placeholder_template : string
- $placeholders : array<string|int, mixed>
- $placeholders_counter : int
- $pos : int|bool
- $pos1 : int|null
- $results : array<string|int, mixed>
Methods
- __construct() : mixed
- Constructor.
- attachValidate() : void
- Validation method for the attach BBCode.
- codeValidate() : void
- Validation method for the code BBCode.
- emailValidate() : void
- Validation method for the email BBCode.
- flashValidate() : void
- Validation method for the flash BBCode.
- floatValidate() : void
- Validation method for the float BBCode.
- ftpValidate() : void
- Validation method for the ftp BBCode.
- getAllTagsRegex() : string
- Gets a regular expression to match all known BBC tags.
- getBBCodes() : array<string|int, mixed>
- Get the list of supported BBCodes, including any added by modifications.
- getBBCodeTagsRegex() : string
- Gets a regular expression to match all known BBC tags.
- getCodes() : array<string|int, mixed>
- Get the list of supported BBCodes, including any added by modifications.
- getSigTags() : array<string|int, mixed>
- Returns an array of BBCodes tags that are allowed in signatures.
- highlightPhpCode() : string
- Highlight any code.
- imgValidate() : void
- Validation method for the img BBCode.
- load() : object
- Returns a reusable instance of this class.
- parse() : string
- Parse bulletin board code in a string.
- phpValidate() : void
- Validation method for the php BBCode.
- shadowValidate() : void
- Validation method for the shadow BBCode.
- sizeValidate() : void
- Validation method for the size BBCode.
- timeValidate() : void
- Validation method for the time BBCode.
- transform() : string
- Transforms one type of markup into another.
- unparse() : string
- Converts HTML to BBC.
- urlValidate() : void
- Validation method for the url and iurl BBCodes.
- closeInlineTags() : void
- Similar to $this->closeTags(), but only for inline tags.
- closeTags() : void
- Ensures BBCode markup is well-formed by auto-closing nested tags in the correct order.
- detectTag() : array<string|int, mixed>
- Figures out which BBCode the current tag is.
- disableCode() : array<string|int, mixed>
- Adjusts a BBCode definition so that it outputs its disabled version.
- fetchTagAttributes() : array<string|int, mixed>
- Helper for unparse().
- fixHtml() : string
- Fixes up any raw HTML in a BBCode string.
- getCacheKey() : string
- Generates a unique cache key for the combination of string, parameters, settings, etc., that apply to this particular call to self::transform().
- highLoadAverage() : bool
- Checks whether the server's load average is too high to parse BBCode.
- insertTxt() : string
- Replaces {txt_*} tokens with Lang::$txt strings.
- legalise() : string
- Helper for unparse().
- parseItemCode() : void
- Parses itemcodes into normal list items.
- parseMessage() : void
- The method that actually parses the BBCode in $this->message.
- resetRuntimeProperties() : void
- Resets certain runtime properties to their default values.
- setAllTagsRegex() : void
- Sets $this->alltags_regex.
- setBbcCodes() : void
- Sets $this->bbc_codes.
- setDisabled() : void
- Sets $this->disabled.
- setOptions() : array<string|int, mixed>
- Fills in any missing elements of $options with the default values.
- setStaticVars() : void
- Sets the values of this class's static variables.
- toBBC() : string
- Transforms the input string into BBCode.
- toHTML() : string
- Transforms the input string into HTML.
- toText() : string
- Transforms the input string into plain text (i.e. removes all markup).
- transformToHtml() : void
- Transforms a BBCode tag into HTML.
- integrateBBC() : void
- Wrapper for the integrate_bbc_codes hook.
Constants
INPUT_BBC
public
int
INPUT_BBC
= 0b1
Indicates that BBCode should be parsed in the input string.
INPUT_MARKDOWN
public
int
INPUT_MARKDOWN
= 0b10
Indicates that Markdown should be parsed in the input string.
INPUT_SMILEYS
public
int
INPUT_SMILEYS
= 0b100
Indicates that smileys should be parsed in the input string.
When the output type is HTML, this controls whether smiley text will
be transformed into tags pointing to smiley images.
When the output type is plain text, this controls whether tags for
smiley images will be transformed into smiley text or removed.
When the output type is BBCode, this controls whether tags for
smiley images will be transformed into smiley text or [img] BBCodes.
OUTPUT_BBC
public
int
OUTPUT_BBC
= 2
Used to set the output to BBCode.
When this is used, HTML and Markdown in the input will be transformed into the equivalent BBCode. Unsupported HTML tags will be removed.
OUTPUT_HTML
public
int
OUTPUT_HTML
= 0
Used to set the output to HTML.
This is the default output type.
OUTPUT_TEXT
public
int
OUTPUT_TEXT
= 1
Used to set the output to plain text.
When this is used, the input will be parsed into HTML and then the HTML tags will be stripped.
Properties
$custom_smileys_enabled
public
static bool
$custom_smileys_enabled
Whether custom smileys are enabled.
$defalt_options
public
static array<string|int, mixed>
$defalt_options
= ['cache_id' => '', 'parse_tags' => [], 'for_print' => false, 'hard_breaks' => null, 'no_paragraphs' => false, 'str_replace' => [], 'preg_replace' => []]
Default options for the various parsers.
-
cache_id: ID string to identify the string for caching purposes. If empty, an ID will be generated automatically. Default: ''
-
parse_tags: A list of specific BBC tags to parse. If empty, all BBC are parsed. Default: []
-
for_print: Whether the output is intended for a non-interactive medium, such as being printed on paper. Default: false
-
hard_breaks: Controls how line breaks are handled by MarkdownParser. For more info, see the documentation for MarkdownParser::__construct(). Default: null
-
str_replace: String replacements to apply when converting to plain text. Keys are the strings to find, and values are the replacements. These replacements are applied after the input has been transformed into HTML and before the HTML tags are stripped out. Default: []
-
preg_replace: Similar to str_replace, except that the keys are regular expressions. Default: []
Mods implementing custom parsers can add values to this array using the integrate_parser_options hook.
$disabled
public
array<string|int, mixed>
$disabled
= []
List of disabled BBCode tags.
$enable_bbc
public
static bool
$enable_bbc
Whether BBCode should be parsed.
$enable_markdown
public
static bool
$enable_markdown
Whether Markdown should be parsed.
$enable_post_html
public
static bool
$enable_post_html
Whether to allow certain basic HTML tags in the input.
$encoding
public
static string
$encoding
The character encoding of the strings to be parsed.
$for_print
public
bool
$for_print
= false
Enables special handling if output is meant for paper printing.
$locale
public
static string
$locale
Language locale to use.
$parse_tags
public
array<string|int, mixed>
$parse_tags
= []
If not empty, only these BBCode tags will be parsed.
$smiley_set
public
static string
$smiley_set
The smiley set to use when parsing smileys.
$smileys_url
public
static string
$smileys_url
URL of the base smileys directory.
$time_format
public
static string
$time_format
User's time format string.
$time_offset
public
static int
$time_offset
User's time offset from UTC.
$alltags_regex
protected
string|null
$alltags_regex
= null
Regular expression to match all BBCode tags.
$codes
protected
static array<string|int, mixed>
$codes
= [
['tag' => 'abbr', 'type' => 'unparsed_equals', 'before' => '<abbr title="$1">', 'after' => '</abbr>', 'quoted' => 'optional', 'disabled_after' => ' ($1)'],
// Legacy (and just an alias for [abbr] even when enabled)
['tag' => 'acronym', 'type' => 'unparsed_equals', 'before' => '<abbr title="$1">', 'after' => '</abbr>', 'quoted' => 'optional', 'disabled_after' => ' ($1)'],
['tag' => 'anchor', 'type' => 'unparsed_equals', 'test' => '[#]?([A-Za-z][A-Za-z0-9_\-]*)\]', 'before' => '<span id="post_$1">', 'after' => '</span>'],
['tag' => 'attach', 'type' => 'unparsed_content', 'parameters' => ['id' => ['match' => '(\d+)'], 'alt' => ['optional' => true], 'width' => ['optional' => true, 'match' => '(\d+)'], 'height' => ['optional' => true, 'match' => '(\d+)'], 'display' => ['optional' => true, 'match' => '(link|embed)']], 'content' => '$1', 'validate' => __CLASS__ . '::attachValidate'],
['tag' => 'b', 'before' => '<strong>', 'after' => '</strong>'],
// Legacy (equivalent to [ltr] or [rtl])
['tag' => 'bdo', 'type' => 'unparsed_equals', 'before' => '<bdo dir="$1">', 'after' => '</bdo>', 'test' => '(rtl|ltr)\]', 'block_level' => true],
// Legacy (alias of [color=black])
['tag' => 'black', 'before' => '<span style="color: black;" class="bbc_color">', 'after' => '</span>'],
// Legacy (alias of [color=blue])
['tag' => 'blue', 'before' => '<span style="color: blue;" class="bbc_color">', 'after' => '</span>'],
[
'tag' => 'br',
'type' => 'closed',
// We put a class on this to force the Markdown parser to preserve it.
'content' => '<br class="bbc_br">',
],
['tag' => 'center', 'before' => '<div class="centertext"><div class="inline-block">', 'after' => '</div></div>', 'block_level' => true],
['tag' => 'code', 'type' => 'unparsed_content', 'content' => '<div class="codeheader"><span class="code">{txt_code}</span> <a class="codeoperation smf_select_text">{txt_code_select}</a> <a class="codeoperation smf_expand_code hidden" data-shrink-txt="{txt_code_shrink}" data-expand-txt="{txt_code_expand}">{txt_code_expand}</a></div><code class="bbc_code">$1</code>', 'validate' => __CLASS__ . '::codeValidate', 'block_level' => true],
['tag' => 'code', 'type' => 'unparsed_equals_content', 'content' => '<div class="codeheader"><span class="code">{txt_code}</span> ($2) <a class="codeoperation smf_select_text">{txt_code_select}</a> <a class="codeoperation smf_expand_code hidden" data-shrink-txt="{txt_code_shrink}" data-expand-txt="{txt_code_expand}">{txt_code_expand}</a></div><code class="bbc_code">$1</code>', 'validate' => __CLASS__ . '::codeValidate', 'block_level' => true],
['tag' => 'color', 'type' => 'unparsed_equals', 'test' => '(#[\da-fA-F]{3}|#[\da-fA-F]{6}|[A-Za-z]{1,20}|rgb\((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\s?,\s?){2}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\))\]', 'before' => '<span style="color: $1;" class="bbc_color">', 'after' => '</span>'],
['tag' => 'email', 'type' => 'unparsed_content', 'content' => '<a href="mailto:$1" class="bbc_email">$1</a>', 'validate' => __CLASS__ . '::emailValidate'],
['tag' => 'email', 'type' => 'unparsed_equals', 'before' => '<a href="mailto:$1" class="bbc_email">', 'after' => '</a>', 'disallow_children' => ['email', 'ftp', 'url', 'iurl'], 'disabled_after' => ' ($1)'],
// Legacy (and just a link even when not disabled)
['tag' => 'flash', 'type' => 'unparsed_commas_content', 'test' => '\d+,\d+\]', 'content' => '<a href="$1" target="_blank" rel="noopener">$1</a>', 'validate' => __CLASS__ . '::flashValidate'],
['tag' => 'float', 'type' => 'unparsed_equals', 'test' => '(left|right)(\s+max=\d+(?:%|px|em|rem|ex|pt|pc|ch|vw|vh|vmin|vmax|cm|mm|in)?)?\]', 'before' => '<div $1>', 'after' => '</div>', 'validate' => __CLASS__ . '::floatValidate', 'trim' => 'outside', 'block_level' => true],
// Legacy (alias of [url] with an FTP URL)
['tag' => 'ftp', 'type' => 'unparsed_content', 'content' => '<a href="$1" class="bbc_link" target="_blank" rel="noopener">$1</a>', 'validate' => __CLASS__ . '::ftpValidate'],
// Legacy (alias of [url] with an FTP URL)
['tag' => 'ftp', 'type' => 'unparsed_equals', 'before' => '<a href="$1" class="bbc_link" target="_blank" rel="noopener">', 'after' => '</a>', 'validate' => __CLASS__ . '::ftpValidate', 'disallow_children' => ['email', 'ftp', 'url', 'iurl'], 'disabled_after' => ' ($1)'],
['tag' => 'font', 'type' => 'unparsed_equals', 'test' => '[A-Za-z0-9_,\-\s]+?\]', 'before' => '<span style="font-family: $1;" class="bbc_font">', 'after' => '</span>'],
// Legacy (one of those things that should not be done)
['tag' => 'glow', 'type' => 'unparsed_commas', 'test' => '[#0-9a-zA-Z\-]{3,12},([012]\d{1,2}|\d{1,2})(,[^]]+)?\]', 'before' => '<span style="text-shadow: $1 1px 1px 1px">', 'after' => '</span>'],
// Legacy (alias of [color=green])
['tag' => 'green', 'before' => '<span style="color: green;" class="bbc_color">', 'after' => '</span>'],
// For the h1-h6 tags, the element name will often change in the final
// output, but the class will not. For example, `<h1 class="bbc_h1">`
// might become `<h5 class="bbc_h1">` in the final output.
['tag' => 'h1', 'before' => '<h1 class="bbc_h1">', 'after' => '</h1>', 'block_level' => true],
['tag' => 'h2', 'before' => '<h2 class="bbc_h2">', 'after' => '</h2>', 'block_level' => true],
['tag' => 'h3', 'before' => '<h3 class="bbc_h3">', 'after' => '</h3>', 'block_level' => true],
['tag' => 'h4', 'before' => '<h4 class="bbc_h4">', 'after' => '</h4>', 'block_level' => true],
['tag' => 'h5', 'before' => '<h5 class="bbc_h5">', 'after' => '</h5>', 'block_level' => true],
['tag' => 'h6', 'before' => '<h6 class="bbc_h6">', 'after' => '</h6>', 'block_level' => true],
['tag' => 'html', 'type' => 'unparsed_content', 'content' => '<div class="bbc_html">$1</div>', 'block_level' => true, 'disabled_content' => '$1'],
['tag' => 'hr', 'type' => 'closed', 'content' => '<hr>', 'block_level' => true],
['tag' => 'i', 'before' => '<em>', 'after' => '</em>'],
['tag' => 'img', 'type' => 'unparsed_content', 'parameters' => ['alt' => ['optional' => true], 'title' => ['optional' => true], 'width' => ['optional' => true, 'value' => ' width="$1"', 'match' => '(\d+)'], 'height' => ['optional' => true, 'value' => ' height="$1"', 'match' => '(\d+)']], 'content' => '$1', 'validate' => __CLASS__ . '::imgValidate', 'disabled_content' => '($1)'],
['tag' => 'iurl', 'type' => 'unparsed_content', 'content' => '<a href="$1" class="bbc_link">$1</a>', 'validate' => __CLASS__ . '::urlValidate'],
['tag' => 'iurl', 'type' => 'unparsed_equals', 'quoted' => 'optional', 'before' => '<a href="$1" class="bbc_link">', 'after' => '</a>', 'validate' => __CLASS__ . '::urlValidate', 'disallow_children' => ['email', 'ftp', 'url', 'iurl'], 'disabled_after' => ' ($1)'],
['tag' => 'justify', 'before' => '<div class="justifytext">', 'after' => '</div>', 'block_level' => true],
['tag' => 'left', 'before' => '<div class="lefttext">', 'after' => '</div>', 'block_level' => true],
['tag' => 'li', 'before' => '<li>', 'after' => '</li>', 'trim' => 'outside', 'require_parents' => ['list'], 'block_level' => true, 'disabled_before' => '', 'disabled_after' => '<br>'],
['tag' => 'list', 'before' => '<ul class="bbc_list">', 'after' => '</ul>', 'trim' => 'inside', 'require_children' => ['li', 'list'], 'block_level' => true],
['tag' => 'list', 'parameters' => ['type' => ['match' => '(none|disc|circle|square)']], 'before' => '<ul class="bbc_list" style="list-style-type: {type};">', 'after' => '</ul>', 'trim' => 'inside', 'require_children' => ['li'], 'block_level' => true],
['tag' => 'list', 'parameters' => ['type' => ['match' => '(decimal|decimal-leading-zero|lower-roman|upper-roman|lower-alpha|upper-alpha|lower-greek|upper-greek|lower-latin|upper-latin|hebrew|armenian|georgian|cjk-ideographic|hiragana|katakana|hiragana-iroha|katakana-iroha)']], 'before' => '<ol class="bbc_list" style="list-style-type: {type};">', 'after' => '</ol>', 'trim' => 'inside', 'require_children' => ['li'], 'block_level' => true],
['tag' => 'ltr', 'before' => '<bdo dir="ltr">', 'after' => '</bdo>', 'block_level' => true],
['tag' => 'me', 'type' => 'unparsed_equals', 'before' => '<div class="meaction">* $1 ', 'after' => '</div>', 'quoted' => 'optional', 'block_level' => true, 'disabled_before' => '/me ', 'disabled_after' => '<br>'],
['tag' => 'member', 'type' => 'unparsed_equals', 'before' => '<a href="{scripturl}?action=profile;u=$1" class="mention" data-mention="$1">@', 'after' => '</a>'],
// Legacy (horrible memories of the 1990s)
['tag' => 'move', 'before' => '<marquee>', 'after' => '</marquee>', 'block_level' => true, 'disallow_children' => ['move']],
['tag' => 'nobbc', 'type' => 'unparsed_content', 'content' => '$1'],
// This one only exists to prevent autolinking in its content.
['tag' => 'nolink', 'before' => '', 'after' => ''],
['tag' => 'php', 'type' => 'unparsed_content', 'content' => '<code class="phpcode">$1</code>', 'validate' => __CLASS__ . '::phpValidate', 'block_level' => false, 'disabled_content' => '$1'],
['tag' => 'pre', 'before' => '<pre>', 'after' => '</pre>'],
['tag' => 'quote', 'before' => '<blockquote><cite>{txt_quote}</cite>', 'after' => '</blockquote>', 'trim' => 'both', 'block_level' => true],
['tag' => 'quote', 'parameters' => ['author' => ['match' => '(.{1,192}?)', 'quoted' => true]], 'before' => '<blockquote><cite>{txt_quote_from}: {author}</cite>', 'after' => '</blockquote>', 'trim' => 'both', 'block_level' => true],
[
'tag' => 'quote',
'type' => 'parsed_equals',
'before' => '<blockquote><cite>{txt_quote_from}: $1</cite>',
'after' => '</blockquote>',
'trim' => 'both',
'quoted' => 'optional',
// Don't allow everything to be embedded with the author name.
'parsed_tags_allowed' => ['url', 'iurl', 'ftp'],
'block_level' => true,
],
['tag' => 'quote', 'parameters' => ['author' => ['match' => '([^<>]{1,192}?)'], 'link' => ['match' => '(?:board=\d+;)?((?:topic|threadid)=[\dmsg#\./]{1,40}(?:;start=[\dmsg#\./]{1,40})?|msg=\d+?|action=profile;u=\d+)'], 'date' => ['match' => '(\d+)', 'validate' => 'SMF\Time::stringFromUnix']], 'before' => '<blockquote><cite><a href="{scripturl}?{link}">{txt_quote_from}: {author} {txt_search_on} {date}</a></cite>', 'after' => '</blockquote>', 'trim' => 'both', 'block_level' => true],
['tag' => 'quote', 'parameters' => ['author' => ['match' => '(.{1,192}?)']], 'before' => '<blockquote><cite>{txt_quote_from}: {author}</cite>', 'after' => '</blockquote>', 'trim' => 'both', 'block_level' => true],
// Legacy (alias of [color=red])
['tag' => 'red', 'before' => '<span style="color: red;" class="bbc_color">', 'after' => '</span>'],
['tag' => 'right', 'before' => '<div class="righttext"><div class="inline-block">', 'after' => '</div></div>', 'block_level' => true],
['tag' => 'rtl', 'before' => '<bdo dir="rtl">', 'after' => '</bdo>', 'block_level' => true],
['tag' => 's', 'before' => '<s>', 'after' => '</s>'],
// Legacy (never a good idea)
['tag' => 'shadow', 'type' => 'unparsed_commas', 'test' => '[#0-9a-zA-Z\-]{3,12},(left|right|top|bottom|[0123]\d{0,2})\]', 'before' => '<span style="text-shadow: $1 $2">', 'after' => '</span>', 'validate' => __CLASS__ . '::shadowValidate'],
['tag' => 'size', 'type' => 'unparsed_equals', 'test' => '([1-9][\d]?p[xt]|small(?:er)?|large[r]?|x[x]?-(?:small|large)|medium|(0\.[1-9]|[1-9](\.[\d][\d]?)?)?em)\]', 'before' => '<span style="font-size: $1;" class="bbc_size">', 'after' => '</span>'],
['tag' => 'size', 'type' => 'unparsed_equals', 'test' => '[1-7]\]', 'before' => '<span style="font-size: $1;" class="bbc_size">', 'after' => '</span>', 'validate' => __CLASS__ . '::sizeValidate'],
['tag' => 'sub', 'before' => '<sub>', 'after' => '</sub>'],
['tag' => 'sup', 'before' => '<sup>', 'after' => '</sup>'],
['tag' => 'table', 'before' => '<table class="bbc_table">', 'after' => '</table>', 'trim' => 'inside', 'require_children' => ['tr'], 'block_level' => true],
['tag' => 'td', 'before' => '<td>', 'after' => '</td>', 'require_parents' => ['tr'], 'trim' => 'outside', 'block_level' => true, 'disabled_before' => '', 'disabled_after' => ''],
['tag' => 'time', 'type' => 'unparsed_equals_content', 'content' => '<time class="bbc_time" datetime="$2">$1</time>', 'validate' => __CLASS__ . '::timeValidate'],
['tag' => 'time', 'type' => 'unparsed_content', 'content' => '<time class="bbc_time" datetime="$1">$1</time>', 'validate' => __CLASS__ . '::timeValidate'],
['tag' => 'tr', 'before' => '<tr>', 'after' => '</tr>', 'require_parents' => ['table'], 'require_children' => ['td'], 'trim' => 'both', 'block_level' => true, 'disabled_before' => '', 'disabled_after' => ''],
['tag' => 'tt', 'before' => '<code class="bbc_tt">', 'after' => '</code>'],
['tag' => 'u', 'before' => '<u>', 'after' => '</u>'],
['tag' => 'url', 'type' => 'unparsed_content', 'content' => '<a href="$1" class="bbc_link" target="_blank" rel="noopener">$1</a>', 'validate' => __CLASS__ . '::urlValidate'],
['tag' => 'url', 'type' => 'unparsed_equals', 'quoted' => 'optional', 'before' => '<a href="$1" class="bbc_link" target="_blank" rel="noopener">', 'after' => '</a>', 'validate' => __CLASS__ . '::urlValidate', 'disallow_children' => ['email', 'ftp', 'url', 'iurl'], 'disabled_after' => ' ($1)'],
// Legacy (alias of [color=white])
['tag' => 'white', 'before' => '<span style="color: white;" class="bbc_color">', 'after' => '</span>'],
['tag' => 'youtube', 'type' => 'unparsed_content', 'content' => '<div class="videocontainer"><div><iframe frameborder="0" src="https://www.youtube.com/embed/$1?origin={hosturl}&wmode=opaque" data-youtube-id="$1" allowfullscreen loading="lazy"></iframe></div></div>', 'disabled_content' => '<a href="https://www.youtube.com/watch?v=$1" target="_blank" rel="noopener">https://www.youtube.com/watch?v=$1</a>', 'block_level' => true],
]
Definitions of supported BBCodes.
The BBCode definitions are formatted as an array, with keys as follows:
tag: The tag's name - should be lowercase!
type: One of... - (missing): [tag]parsed content[/tag] - unparsed_equals: [tag=xyz]parsed content[/tag] - parsed_equals: [tag=parsed data]parsed content[/tag] - unparsed_content: [tag]unparsed content[/tag] - closed: [tag], [tag/], [tag /] - unparsed_commas: [tag=1,2,3]parsed content[/tag] - unparsed_commas_content: [tag=1,2,3]unparsed content[/tag] - unparsed_equals_content: [tag=...]unparsed content[/tag]
parameters: An optional array of parameters, for the form [tag abc=123]content[/tag]. The array is an associative array where the keys are the parameter names, and the values are an array which may contain the following: - match: a regular expression to validate and match the value. - quoted: true if the value should be quoted. - validate: callback to evaluate on the data, which is $data. - value: a string in which to replace $1 with the data. Either value or validate may be used, not both. - optional: true if the parameter is optional. - default: a default value for missing optional parameters.
test: A regular expression to test immediately after the tag's '=', ' ' or ']'. Typically, should have a ] at the end. Optional.
content: Only available for unparsed_content, closed, unparsed_commas_content, and unparsed_equals_content. $1 is replaced with the content of the tag. Parameters are replaced in the form {param}. For unparsed_commas_content, $2, $3, ..., $n are replaced. The form {txt_*} can be used to insert Lang::$txt strings, e.g. {txt_code} will be replaced with the value of Lang::$txt['code'].
before: Only when content is not used, to go before any content. For unparsed_equals, $1 is replaced with the value. For unparsed_commas, $1, $2, ..., $n are replaced.
after: Similar to before in every way, except that it is used when the tag is closed.
disabled_content: Used in place of content when the tag is disabled. For closed, default is '', otherwise it is '$1' if block_level is false, '
disabled_before: Used in place of before when disabled. Defaults to '
disabled_after: Used in place of after when disabled. Defaults to '
block_level: Set to true the tag is a "block level" tag, similar to HTML. Block level tags cannot be nested inside tags that are not block level, and will not be implicitly closed as easily. One break following a block level tag may also be removed.
trim: If set to 'inside', whitespace after the begin tag will be removed. If set to 'outside', whitespace after the end tag will meet the same fate.
validate: A callback to validate the data as $data. Four arguments will be passed to the callback: &$tag, &$data, $disabled, $params. Depending on the tag's type, $data may be a string or an array of strings (corresponding to the replacement.)
quoted: When type is 'unparsed_equals' or 'parsed_equals' only, may be not set, 'optional', or 'required' corresponding to if the content may be quoted. This allows the parser to read [tag="abc]def[esdf]"] properly.
require_parents: An array of tag names, or not set. If set, the enclosing tag must be one of the listed tags, or parsing won't occur.
require_children: Similar to require_parents, if set children won't be parsed if they are not in the list.
disallow_children: Similar to, but very different from, require_children, if it is set the listed tags will not be parsed inside the tag.
parsed_tags_allowed: An array restricting what BBC can be in the parsed_equals parameter, if desired.
$itemcodes
protected
static array<string|int, mixed>
$itemcodes
= ['*' => 'disc', '@' => 'disc', '+' => 'square', 'x' => 'square', '#' => 'square', 'o' => 'circle', 'O' => 'circle', '0' => 'circle']
Itemcodes are an alternative syntax for creating lists.
$smileys
protected
bool
$smileys
= true
Whether smileys should be parsed while we are parsing BBCode.
$bbc_codes
private
array<string|int, mixed>
$bbc_codes
= []
Version of self::$codes used for internal processing.
$bbc_lang_locales
private
array<string|int, mixed>
$bbc_lang_locales
= []
Copies of $this->bbc_codes for different locales.
$hosturl
private
string
$hosturl
URL of this host/domain. Needed for the YouTube BBCode.
$inside
private
array<string|int, mixed>|null
$inside
= null
The last item of $this->open_tags.
$integrate_bbc_codes_done
private
static bool
$integrate_bbc_codes_done
= false
Tracks whether the integration_bbc_codes hook was called.
$last_pos
private
int
$last_pos
= null
Previous value of $this->pos.
$message
private
string
$message
= ''
The string in which to parse BBCode.
$open_tags
private
array<string|int, mixed>
$open_tags
= []
BBCode tags that are currently open at any given step of processing $this->message.
$parsers
private
static array<string|int, mixed>
$parsers
= []
Reusable instances of this class.
$placeholder_template
private
string
$placeholder_template
= "" . '%1$s' . ""
The sprintf format used to create placeholders. Uses private use Unicode characters to prevent conflicts.
$placeholders
private
array<string|int, mixed>
$placeholders
= []
Placeholders used to protect certain strings from processing.
$placeholders_counter
private
int
$placeholders_counter
= 0
How many placeholders we have created.
$pos
private
int|bool
$pos
= -1
Current position in $this->message.
$pos1
private
int|null
$pos1
= null
Position where current BBCode tag ends.
$results
private
static array<string|int, mixed>
$results
= []
Holds parsed messages.
Methods
__construct()
Constructor.
public
__construct([bool $for_print = false ]) : mixed
Parameters
- $for_print : bool = false
attachValidate()
Validation method for the attach BBCode.
public
static attachValidate(array<string|int, mixed> &$tag, array<string|int, mixed>|string &$data, array<string|int, mixed> $disabled, array<string|int, mixed> $params) : void
Parameters
- $tag : array<string|int, mixed>
-
A copy of this tag's definition.
- $data : array<string|int, mixed>|string
-
The data in this particular BBCode instance.
- $disabled : array<string|int, mixed>
-
List of disabled BBCodes.
- $params : array<string|int, mixed>
-
Parameters supplied in this BBCode instance.
codeValidate()
Validation method for the code BBCode.
public
static codeValidate(array<string|int, mixed> &$tag, array<string|int, mixed>|string &$data, array<string|int, mixed> $disabled, array<string|int, mixed> $params) : void
Parameters
- $tag : array<string|int, mixed>
-
A copy of this tag's definition.
- $data : array<string|int, mixed>|string
-
The data in this particular BBCode instance.
- $disabled : array<string|int, mixed>
-
List of disabled BBCodes.
- $params : array<string|int, mixed>
-
Parameters supplied in this BBCode instance.
emailValidate()
Validation method for the email BBCode.
public
static emailValidate(array<string|int, mixed> &$tag, array<string|int, mixed>|string &$data, array<string|int, mixed> $disabled, array<string|int, mixed> $params) : void
Parameters
- $tag : array<string|int, mixed>
-
A copy of this tag's definition.
- $data : array<string|int, mixed>|string
-
The data in this particular BBCode instance.
- $disabled : array<string|int, mixed>
-
List of disabled BBCodes.
- $params : array<string|int, mixed>
-
Parameters supplied in this BBCode instance.
Tags
flashValidate()
Validation method for the flash BBCode.
public
static flashValidate(array<string|int, mixed> &$tag, array<string|int, mixed>|string &$data, array<string|int, mixed> $disabled, array<string|int, mixed> $params) : void
Parameters
- $tag : array<string|int, mixed>
-
A copy of this tag's definition.
- $data : array<string|int, mixed>|string
-
The data in this particular BBCode instance.
- $disabled : array<string|int, mixed>
-
List of disabled BBCodes.
- $params : array<string|int, mixed>
-
Parameters supplied in this BBCode instance.
floatValidate()
Validation method for the float BBCode.
public
static floatValidate(array<string|int, mixed> &$tag, array<string|int, mixed>|string &$data, array<string|int, mixed> $disabled, array<string|int, mixed> $params) : void
Parameters
- $tag : array<string|int, mixed>
-
A copy of this tag's definition.
- $data : array<string|int, mixed>|string
-
The data in this particular BBCode instance.
- $disabled : array<string|int, mixed>
-
List of disabled BBCodes.
- $params : array<string|int, mixed>
-
Parameters supplied in this BBCode instance.
ftpValidate()
Validation method for the ftp BBCode.
public
static ftpValidate(array<string|int, mixed> &$tag, array<string|int, mixed>|string &$data, array<string|int, mixed> $disabled, array<string|int, mixed> $params) : void
Parameters
- $tag : array<string|int, mixed>
-
A copy of this tag's definition.
- $data : array<string|int, mixed>|string
-
The data in this particular BBCode instance.
- $disabled : array<string|int, mixed>
-
List of disabled BBCodes.
- $params : array<string|int, mixed>
-
Parameters supplied in this BBCode instance.
getAllTagsRegex()
Gets a regular expression to match all known BBC tags.
public
getAllTagsRegex() : string
Return values
string —A copy of $this->alltags_regex.
getBBCodes()
Get the list of supported BBCodes, including any added by modifications.
public
static getBBCodes() : array<string|int, mixed>
Return values
array<string|int, mixed> —List of supported BBCodes.
getBBCodeTagsRegex()
Gets a regular expression to match all known BBC tags.
public
static getBBCodeTagsRegex() : string
Return values
string —Regular expression to match all BBCode tags.
getCodes()
Get the list of supported BBCodes, including any added by modifications.
public
static getCodes() : array<string|int, mixed>
Return values
array<string|int, mixed> —List of supported BBCodes.
getSigTags()
Returns an array of BBCodes tags that are allowed in signatures.
public
static getSigTags() : array<string|int, mixed>
Return values
array<string|int, mixed> —An array containing allowed tags for signatures, or an empty array if all tags are allowed.
highlightPhpCode()
Highlight any code.
public
static highlightPhpCode(string $code) : string
Uses PHP's highlight_string() to highlight PHP syntax. Does special handling to keep the tabs in the code available. Used to parse PHP code from inside [code] and [php] tags.
Parameters
- $code : string
-
The code.
Return values
string —The code with highlighted HTML.
imgValidate()
Validation method for the img BBCode.
public
static imgValidate(array<string|int, mixed> &$tag, array<string|int, mixed>|string &$data, array<string|int, mixed> $disabled, array<string|int, mixed> $params) : void
Parameters
- $tag : array<string|int, mixed>
-
A copy of this tag's definition.
- $data : array<string|int, mixed>|string
-
The data in this particular BBCode instance.
- $disabled : array<string|int, mixed>
-
List of disabled BBCodes.
- $params : array<string|int, mixed>
-
Parameters supplied in this BBCode instance.
load()
Returns a reusable instance of this class.
public
static load([bool $for_print = false ]) : object
Using this method to get a BBCodeParser instance saves memory by avoiding creating redundant instances.
Parameters
- $for_print : bool = false
-
If true, adjusts output for print media.
Return values
object —An instance of this class.
parse()
Parse bulletin board code in a string.
public
parse(string|bool $message[, bool $smileys = true ][, string|int $cache_id = '' ][, array<string|int, mixed> $parse_tags = [] ]) : string
Parameters
- $message : string|bool
-
The string to parse.
- $smileys : bool = true
-
Whether to parse smileys. Default: true.
- $cache_id : string|int = ''
-
The cache ID. If $cache_id is left empty, an ID will be generated automatically. Manually specifying a ID is helpful in cases when an integration hook wants to identify particular strings to act upon, but is otherwise unnecessary.
- $parse_tags : array<string|int, mixed> = []
-
If set, only parses these tags rather than all of them.
Return values
string —The parsed string.
phpValidate()
Validation method for the php BBCode.
public
static phpValidate(array<string|int, mixed> &$tag, array<string|int, mixed>|string &$data, array<string|int, mixed> $disabled, array<string|int, mixed> $params) : void
Parameters
- $tag : array<string|int, mixed>
-
A copy of this tag's definition.
- $data : array<string|int, mixed>|string
-
The data in this particular BBCode instance.
- $disabled : array<string|int, mixed>
-
List of disabled BBCodes.
- $params : array<string|int, mixed>
-
Parameters supplied in this BBCode instance.
shadowValidate()
Validation method for the shadow BBCode.
public
static shadowValidate(array<string|int, mixed> &$tag, array<string|int, mixed>|string &$data, array<string|int, mixed> $disabled, array<string|int, mixed> $params) : void
Parameters
- $tag : array<string|int, mixed>
-
A copy of this tag's definition.
- $data : array<string|int, mixed>|string
-
The data in this particular BBCode instance.
- $disabled : array<string|int, mixed>
-
List of disabled BBCodes.
- $params : array<string|int, mixed>
-
Parameters supplied in this BBCode instance.
sizeValidate()
Validation method for the size BBCode.
public
static sizeValidate(array<string|int, mixed> &$tag, array<string|int, mixed>|string &$data, array<string|int, mixed> $disabled, array<string|int, mixed> $params) : void
Parameters
- $tag : array<string|int, mixed>
-
A copy of this tag's definition.
- $data : array<string|int, mixed>|string
-
The data in this particular BBCode instance.
- $disabled : array<string|int, mixed>
-
List of disabled BBCodes.
- $params : array<string|int, mixed>
-
Parameters supplied in this BBCode instance.
timeValidate()
Validation method for the time BBCode.
public
static timeValidate(array<string|int, mixed> &$tag, array<string|int, mixed>|string &$data, array<string|int, mixed> $disabled, array<string|int, mixed> $params) : void
Parameters
- $tag : array<string|int, mixed>
-
A copy of this tag's definition.
- $data : array<string|int, mixed>|string
-
The data in this particular BBCode instance.
- $disabled : array<string|int, mixed>
-
List of disabled BBCodes.
- $params : array<string|int, mixed>
-
Parameters supplied in this BBCode instance.
transform()
Transforms one type of markup into another.
public
static transform(string $string[, int $input_types = self::INPUT_BBC | self::INPUT_MARKDOWN | self::INPUT_SMILEYS ][, int $output_type = self::OUTPUT_HTML ][, array<string|int, mixed> $options = [] ]) : string
Supported input markup types are BBCode, Markdown, and smileys. Supported output markup types are HTML, BBCode, and plain text.
Parameters
- $string : string
-
The string in which to transform markup.
- $input_types : int = self::INPUT_BBC | self::INPUT_MARKDOWN | self::INPUT_SMILEYS
-
Bitmask of this class's INPUT_* constants. Only the indicated types of markup will be parsed in the input string. Default: self::INPUT_BBC | self::INPUT_MARKDOWN | self::INPUT_SMILEYS
- $output_type : int = self::OUTPUT_HTML
-
One of this class's INPUT_* constants. Default: self::OUTPUT_HTML
- $options : array<string|int, mixed> = []
-
Various parser options. See self::$default_options.
Return values
string —The transformed string.
unparse()
Converts HTML to BBC.
public
unparse(string $string) : string
Only used by ManageBoards.php (and possibly mods).
Parameters
- $string : string
-
Text containing HTML.
Return values
string —The string with HTML converted to BBC.
urlValidate()
Validation method for the url and iurl BBCodes.
public
static urlValidate(array<string|int, mixed> &$tag, array<string|int, mixed>|string &$data, array<string|int, mixed> $disabled, array<string|int, mixed> $params) : void
Parameters
- $tag : array<string|int, mixed>
-
A copy of this tag's definition.
- $data : array<string|int, mixed>|string
-
The data in this particular BBCode instance.
- $disabled : array<string|int, mixed>
-
List of disabled BBCodes.
- $params : array<string|int, mixed>
-
Parameters supplied in this BBCode instance.
closeInlineTags()
Similar to $this->closeTags(), but only for inline tags.
protected
closeInlineTags() : void
Operates directly on $this->message.
closeTags()
Ensures BBCode markup is well-formed by auto-closing nested tags in the correct order.
protected
closeTags() : void
Operates directly on $this->message.
detectTag()
Figures out which BBCode the current tag is.
protected
detectTag(string $tag_character) : array<string|int, mixed>
Parameters
- $tag_character : string
-
The first character of this (possible) tag.
Return values
array<string|int, mixed> —The tag's definition and the parameter values to use.
disableCode()
Adjusts a BBCode definition so that it outputs its disabled version.
protected
disableCode(array<string|int, mixed> $code) : array<string|int, mixed>
Parameters
- $code : array<string|int, mixed>
-
A BBCode definition.
Return values
array<string|int, mixed> —The disabled version of the BBCode definition.
fetchTagAttributes()
Helper for unparse().
protected
fetchTagAttributes(string $string) : array<string|int, mixed>
Returns an array of attributes associated with a tag.
Parameters
- $string : string
-
A tag
Return values
array<string|int, mixed> —An array of attributes
fixHtml()
Fixes up any raw HTML in a BBCode string.
protected
fixHtml(string $data) : string
Parameters
- $data : string
-
A string that might contain HTML.
Return values
string —The fixed version of the string.
getCacheKey()
Generates a unique cache key for the combination of string, parameters, settings, etc., that apply to this particular call to self::transform().
protected
static getCacheKey(string $string, int $input_types, int $output_type, array<string|int, mixed> $options) : string
Parameters
- $string : string
-
The string in which to transform markup.
- $input_types : int
-
Bitmask of this class's INPUT_* constants.
- $output_type : int
-
One of this class's INPUT_* constants.
- $options : array<string|int, mixed>
-
An array of parser options.
Return values
string —A unique cache key.
highLoadAverage()
Checks whether the server's load average is too high to parse BBCode.
protected
highLoadAverage() : bool
Return values
bool —Whether the load average is too high.
insertTxt()
Replaces {txt_*} tokens with Lang::$txt strings.
protected
insertTxt(string $string) : string
Parameters
- $string : string
Return values
string —The string with Lang::$txt string values.
legalise()
Helper for unparse().
protected
legalise(string $string) : string
Attempt to clean up illegal BBC caused by browsers like Opera that don't obey the rules.
Parameters
- $string : string
-
Text
Return values
string —Cleaned up text
parseItemCode()
Parses itemcodes into normal list items.
protected
parseItemCode() : void
Operates directly on $this->message.
parseMessage()
The method that actually parses the BBCode in $this->message.
protected
parseMessage() : void
resetRuntimeProperties()
Resets certain runtime properties to their default values.
protected
resetRuntimeProperties() : void
setAllTagsRegex()
Sets $this->alltags_regex.
protected
setAllTagsRegex() : void
setBbcCodes()
Sets $this->bbc_codes.
protected
setBbcCodes() : void
setDisabled()
Sets $this->disabled.
protected
setDisabled() : void
setOptions()
Fills in any missing elements of $options with the default values.
protected
static setOptions(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
-
An array of parser options.
Return values
array<string|int, mixed> —An updated copy of $options.
setStaticVars()
Sets the values of this class's static variables.
protected
static setStaticVars() : void
If a variable already has a value, the existing value is not changed. This ensures that custom values set by external code are respected.
toBBC()
Transforms the input string into BBCode.
protected
static toBBC(string $string, int $input_types, array<string|int, mixed> $options) : string
- Markdown is transformed to the equivalent BBCode.
- HTML img tags for smileys are transformed to smiley text.
- Other HTML is transformed to the equivalent BBCode where possible.
- HTML tags that cannot be transformed are removed.
Parameters
- $string : string
-
The string in which to remove markup.
- $input_types : int
-
Bitmask of this class's INPUT_* constants. Only the indicated types of markup will be parsed in the input string.
- $options : array<string|int, mixed>
-
An array of parser options.
Return values
string —The transformed string.
toHTML()
Transforms the input string into HTML.
protected
static toHTML(string $string, int $input_types, array<string|int, mixed> $options) : string
Parameters
- $string : string
-
The string in which to transform markup.
- $input_types : int
-
Bitmask of this class's INPUT_* constants. Only the indicated types of markup will be parsed in the input string.
- $options : array<string|int, mixed>
-
An array of parser options.
Return values
string —The transformed string.
toText()
Transforms the input string into plain text (i.e. removes all markup).
protected
static toText(string $string, int $input_types, array<string|int, mixed> $options) : string
Parameters
- $string : string
-
The string in which to remove markup.
- $input_types : int
-
Bitmask of this class's INPUT_* constants. Only the indicated types of markup will be parsed in the input string.
- $options : array<string|int, mixed>
-
An array of parser options.
Return values
string —The transformed string.
transformToHtml()
Transforms a BBCode tag into HTML.
protected
transformToHtml(array<string|int, mixed> $tag, array<string|int, mixed> $params) : void
Operates directly on $this->message.
Parameters
- $tag : array<string|int, mixed>
-
The tag definition.
- $params : array<string|int, mixed>
-
Parameter values to use.
integrateBBC()
Wrapper for the integrate_bbc_codes hook.
private
static integrateBBC() : void
Prevents duplication in self::$codes.