You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
5.4 KiB
176 lines
5.4 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* @link https://github.com/genert/bbcode/blob/master/src/Parser/BBCodeParser.php
|
|
* @license The MIT License (MIT)
|
|
* @copyright (c) Genert 2017 - present.
|
|
*
|
|
* Take BB code 2 HTML, to display output
|
|
*/
|
|
|
|
namespace tts\extras;
|
|
|
|
final class bb_code_parser {
|
|
protected $parsers = [
|
|
'h1' => [
|
|
'pattern' => '/\[h1\](.*?)\[\/h1\]/s',
|
|
'replace' => '<h1>$1</h1>',
|
|
'content' => '$1'
|
|
],
|
|
'h2' => [
|
|
'pattern' => '/\[h2\](.*?)\[\/h2\]/s',
|
|
'replace' => '<h2>$1</h2>',
|
|
'content' => '$1'
|
|
],
|
|
'h3' => [
|
|
'pattern' => '/\[h3\](.*?)\[\/h3\]/s',
|
|
'replace' => '<h3>$1</h3>',
|
|
'content' => '$1'
|
|
],
|
|
'h4' => [
|
|
'pattern' => '/\[h4\](.*?)\[\/h4\]/s',
|
|
'replace' => '<h4>$1</h4>',
|
|
'content' => '$1'
|
|
],
|
|
'h5' => [
|
|
'pattern' => '/\[h5\](.*?)\[\/h5\]/s',
|
|
'replace' => '<h5>$1</h5>',
|
|
'content' => '$1'
|
|
],
|
|
'h6' => [
|
|
'pattern' => '/\[h6\](.*?)\[\/h6\]/s',
|
|
'replace' => '<h6>$1</h6>',
|
|
'content' => '$1'
|
|
],
|
|
'bold' => [
|
|
'pattern' => '/\[b\](.*?)\[\/b\]/s',
|
|
'replace' => '<b>$1</b>',
|
|
'content' => '$1'
|
|
],
|
|
'italic' => [
|
|
'pattern' => '/\[i\](.*?)\[\/i\]/s',
|
|
'replace' => '<i>$1</i>',
|
|
'content' => '$1'
|
|
],
|
|
'underline' => [
|
|
'pattern' => '/\[u\](.*?)\[\/u\]/s',
|
|
'replace' => '<u>$1</u>',
|
|
'content' => '$1'
|
|
],
|
|
'strikethrough' => [
|
|
'pattern' => '/\[s\](.*?)\[\/s\]/s',
|
|
'replace' => '<s>$1</s>',
|
|
'content' => '$1'
|
|
],
|
|
'quote' => [
|
|
'pattern' => '/\[quote\](.*?)\[\/quote\]/s',
|
|
'replace' => '<blockquote>$1</blockquote>',
|
|
'content' => '$1'
|
|
],
|
|
'link' => [
|
|
'pattern' => '/\[url\](.*?)\[\/url\]/s',
|
|
'replace' => '<a href="$1">$1</a>',
|
|
'content' => '$1'
|
|
],
|
|
'namedlink' => [
|
|
'pattern' => '/\[url\=(.*?)\](.*?)\[\/url\]/s',
|
|
'replace' => '<a href="$1">$2</a>',
|
|
'content' => '$2'
|
|
],
|
|
'image' => [
|
|
'pattern' => '/\[img\](.*?)\[\/img\]/s',
|
|
'replace' => '<img src="$1">',
|
|
'content' => '$1'
|
|
],
|
|
'orderedlistnumerical' => [
|
|
'pattern' => '/\[list=1\](.*?)\[\/list\]/s',
|
|
'replace' => '<ol>$1</ol>',
|
|
'content' => '$1'
|
|
],
|
|
'orderedlistalpha' => [
|
|
'pattern' => '/\[list=a\](.*?)\[\/list\]/s',
|
|
'replace' => '<ol type="a">$1</ol>',
|
|
'content' => '$1'
|
|
],
|
|
'unorderedlist' => [
|
|
'pattern' => '/\[list\](.*?)\[\/list\]/s',
|
|
'replace' => '<ul>$1</ul>',
|
|
'content' => '$1'
|
|
],
|
|
'listitem' => [
|
|
'pattern' => '/\[\*\](.*)/',
|
|
'replace' => '<li>$1</li>',
|
|
'content' => '$1'
|
|
],
|
|
'code' => [
|
|
'pattern' => '/\[code\](.*?)\[\/code\]/s',
|
|
'replace' => '<code>$1</code>',
|
|
'content' => '$1'
|
|
],
|
|
'youtube' => [
|
|
'pattern' => '/\[youtube\](.*?)\[\/youtube\]/s',
|
|
'replace' => '<iframe width="560" height="315" src="//www.youtube-nocookie.com/embed/$1" frameborder="0" allowfullscreen></iframe>',
|
|
'content' => '$1'
|
|
],
|
|
'sub' => [
|
|
'pattern' => '/\[sub\](.*?)\[\/sub\]/s',
|
|
'replace' => '<sub>$1</sub>',
|
|
'content' => '$1'
|
|
],
|
|
'sup' => [
|
|
'pattern' => '/\[sup\](.*?)\[\/sup\]/s',
|
|
'replace' => '<sup>$1</sup>',
|
|
'content' => '$1'
|
|
],
|
|
'small' => [
|
|
'pattern' => '/\[small\](.*?)\[\/small\]/s',
|
|
'replace' => '<small>$1</small>',
|
|
'content' => '$1'
|
|
],
|
|
'table' => [
|
|
'pattern' => '/\[table\](.*?)\[\/table\]/s',
|
|
'replace' => '<table>$1</table>',
|
|
'content' => '$1',
|
|
],
|
|
'table-row' => [
|
|
'pattern' => '/\[tr\](.*?)\[\/tr\]/s',
|
|
'replace' => '<tr>$1</tr>',
|
|
'content' => '$1',
|
|
],
|
|
'table-data' => [
|
|
'pattern' => '/\[td\](.*?)\[\/td\]/s',
|
|
'replace' => '<td>$1</td>',
|
|
'content' => '$1',
|
|
],
|
|
];
|
|
|
|
private function searchAndReplace(string $pattern, string $replace, string $source): string {
|
|
while (preg_match($pattern, $source)) {
|
|
$source = preg_replace($pattern, $replace, $source);
|
|
}
|
|
|
|
return $source;
|
|
}
|
|
|
|
public function stripTags(string $source): string {
|
|
foreach ($this->parsers as $name => $parser) {
|
|
$source = $this->searchAndReplace($parser['pattern'] . 'i', $parser['content'], $source);
|
|
}
|
|
|
|
return $source;
|
|
}
|
|
|
|
public function parse(string $source, $caseInsensitive = null): string {
|
|
$caseInsensitive = $caseInsensitive === self::CASE_INSENSITIVE ? true : false;
|
|
|
|
foreach ($this->parsers as $name => $parser) {
|
|
$pattern = ($caseInsensitive) ? $parser['pattern'] . 'i' : $parser['pattern'];
|
|
|
|
$source = $this->searchAndReplace($pattern, $parser['replace'], $source);
|
|
}
|
|
|
|
return $source;
|
|
}
|
|
}
|
|
|