dotarb/dotparser

Parse your BBCode easy with this library.


Keywords
parser, laravel, bbcode, PSR-2, PSR-1, PSR-4
License
MIT

Documentation

The dotarb/dotparser package will help you with parsing BBCode.

Install

Via Composer

$ composer require dotarb/dotparser

Usage

To parse some text it's as easy as this!

$bbcode = new Dotarb\BBCode\Parser;

echo $bbcode->parse('[b]Bold Text![/b]');
// <strong>Bold Text!</strong>

Would like the parser to not use all bbcodes? Just do like this.

$bbcode = new Dotarb\BBCode\Parser;

echo $bbcode->only('bold', 'italic')
            ->parse('[b][u]Bold[/u] [i]Italic[/i]![/b]');
            // <strong>[u]Bold[/u] <em>Italic</em>!</strong>

echo $bbcode->except('bold')
            ->parse('[b]Bold[/b] [i]Italic[/i]');
            // [b]Bold[/b] <em>Italic</em>

By default the parser is case sensitive. But if you would like the parser to accept tags like [B]Bold Text[/B] it's really easy.

$bbcode = new Dotarb\BBCode\Parser;

// Case insensitive
echo $bbcode->parse('[b]Bold[/b] [I]Italic![/I]', true);
     // <strong>Bold</strong> <em>Italic!</em>

// Or like this

echo $bbcode->parseCaseInsensitive('[b]Bold[/b] [i]Italic[/i]');
     // <strong>Bold</strong> <em>Italic!</em>

You could also make it more explicit that the parser is case sensitive by using another helper function.

    $bbcode = new Dotarb\BBCode\Parser;

    echo $bbcode->parseCaseSensitive('[b]Bold[/b] [I]Italic![/I]');
         // <strong>Bold</strong> [I]Italic![/I]

If you would like to completely remove all BBCode it's just one function call away.

    $bbcode = new Dotarb\BBCode\Parser;

    echo $bbcode->stripBBCodeTags('[b]Bold[/b] [i]Italic![/i]');
         // Bold Italic!

Laravel integration

The integration into Laravel is really easy, and the method is the same for both Laravel 4 and Laravel 5. Just open your app.php config file.

In there you just add this to your providers array

Dotarb\BBCode\ParserServiceProvider::class,

And this to your facades array

'BBParser' => Dotarb\BBCode\Facades\BBParser::class,

and run publish vendor for package.

php artisan vendor:publish

now you can edit the inital replace text values and add more tags. and can set tags edit mode from database in config/bbcode.php set database to ture and migrate table.

php artisan migrate

The syntax is the same as if you would use it in vanilla PHP but with the BBParser:: before the methods. Here are some examples.

// laravel app.
echo app('bbcode')->parse('[b]Bold Text![/b]');

// Simple parsing
echo BBParser::parse('[b]Bold Text![/b]');

// Limiting the parsers with the only method
echo BBParser::only('bold', 'italic')
        ->parse('[b][u]Bold[/u] [i]Italic[/i]![/b]');
        // <strong>[u]Bold[/u] <em>Italic</em>!</strong>

// Or the except method
echo BBParser::except('bold')
        ->parse('[b]Bold[/b] [i]Italic[/i]');
        // [b]Bold[/b] <em>Italic</em>

supported bbcode tags

tag name bbcode
bold [b]some text[/b]
italic [i]some text[/i]
underline [u]some text[/u]
linethrough [s]some text[/s]
size [size=100] text size is 100 [/size]
color [color=#fff] white text color [/color]
namedcolor [color=white]white text color[/color]
center [center]center text[/center]
left [left]left[/left]
right [right]right[/right]
quote [quote]quote
namedquote [quote=note]quote
link [url]https://www.google.com[/url]
namedlink [url=https://www.google.com]
image [img]https://avatars2.githubusercontent.com/u/26126513?v=3&amp;s=460[/img]
orderedlistnumerical [list=1][*]first[/list]
orderedlistalpha [list=a][*]first[/list]
unorderedlist [list][*]firstelement[/list]
listitem [*]list Item
code [code][/code]
namedcode [code=php][/code]
youtube [youtube]YbRljqNYTYQ[/youtube]
sub [sub]sub text[/sub]
sup [sup]sup text[/sup]
small [small]small text[/small]
table [table][tr][td]table content[/td][/tr][/table]
tr [tr]tr[/tr]
td [td]td[/td]
thead [thead][th][td]table content[/td][/th][/thead]
th [th]th[/th]
fieldset [fieldset]some text[/fieldset]
fieldsetnamed [fieldset=title]this is fieldset with legend[/fieldset]
hr [hr][/hr]

bbcode topic example

[b][center][size=150]BBCode[/size][/center]
BBCode[/b] or [b]Bulletin Board Code[/b] is a [url=https://en.wikipedia.org/wiki/Lightweight_markup_language]lightweight markup language[/url] used to format posts in many [url=https://en.wikipedia.org/wiki/Message_board]message boards[/url]. The available tags are usually indicated by [url=https://en.wikipedia.org/wiki/Bracket]square brackets[/url] ([ ]) surrounding a keyword, and they are [url=https://en.wikipedia.org/wiki/Parsing]parsed[/url] by the message board system before being translated into a [url=https://en.wikipedia.org/wiki/Markup_language]markup language[/url] that [url=https://en.wikipedia.org/wiki/Web_browser]web browsers[/url] understand—usually [url=https://en.wikipedia.org/wiki/HTML]HTML[/url] or [url=https://en.wikipedia.org/wiki/XHTML]XHTML[/url].[sup][url=https://en.wikipedia.org/wiki/BBCode#cite_note-1][1][/url][/sup]
BBCode was introduced in 1998 by the [url=https://en.wikipedia.org/wiki/Messageboard]messageboard[/url] software Ultimate Bulletin Board (UBB) implemented in Perl. In 2000 BBCode was used in [url=https://en.wikipedia.org/wiki/PhpBB]phpBB[/url]—an internet forum system written in [url=https://en.wikipedia.org/wiki/PHP]PHP[/url]. [url=https://en.wikipedia.org/wiki/VBulletin]vBulletin[/url] also uses BBCode.

License

The MIT License (MIT). Please see License File for more information.