towzero/bbcode

Parse your BBCode easy with this library.


Keywords
bbcode, laravel, parser, php
License
MIT

Documentation

The Towzero\BBCode package will help you with parsing BBCode.

Install

Via Composer

$ composer require towzero/bbcode

Usage

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

$bbcode = new Towzero\BBCode\BBCodeParser;

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 Towzero\BBCode\BBCodeParser;

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 Towzero\BBCode\BBCodeParser;

// 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 Towzero\BBCode\BBCodeParser;

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 Towzero\BBCode\BBCodeParser;

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

Laravel integration

Laravel 5.5+:

Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

Laravel 5.4 & don't use auto-discovery:

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

Towzero\BBCode\BBCodeParserServiceProvider::class,

And this to your facades array

'BBCode' => Towzero\BBCode\Facades\BBCodeParser::class,

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

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

// Limiting the parsers with the only method
echo BBCode::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 BBCode::except('bold')
        ->parse('[b]Bold[/b] [i]Italic[/i]');
        // [b]Bold[/b] <em>Italic</em>

Helpers

  • bb_parse()
  • bb_strip()
  • bb_case_sensitive()
  • bb_case_insensitive()
  • bb_only()
  • bb_except()

Initial supported tags

You can show list of bbcode tags in artisan command.

php artisan bbcode:list
Name Syntax Usage
font [font=fontname]Some text[/font] Changes the font of text
bold [b]Some text[/b] Makes text bold
italic [i]Some text[/i] Makes text italic
underline [u]Some text[/u] Underlines text
line-through [s]Some text[/s] Create a strike through on text
size [size=number]Some text[/size] Changes the font-size of text
color [color=hex]Some text[/color] Changes the font-color of text
center [center]Some text[/center] Centers text on screen
left [left]Some text[/left] Left aligns text on screen
right [right]Some text[/right] Right aligns text on screen
quote [quote]Some text[/quote] Creates a quotation box containing text
named-quote [quote=name]Some text[/quote] Creates a quotation box quoting name as saying text
link [url]http://www.someurl.com[/url] Makes a link to url
named-link [url=http://www.someurl.com]Some url click here![/url] Makes a named link to url
image [img]http://www.someurl.com/img.png[/img] Shows the image indicated by
ordered-list-numerical [list=1]items[/list] Displays a ordered numerical list of items
ordered-list-alpha [list=a]items[/list] Displays a ordered alpha list of items
unordered-list [list]items[/list] Displays a unordered list of items
list-item [li]Some text[/li] Specifies an item within a list
code [code]Some text[/code] Meant for rendering code snippets
youtube [youtube]id[/youtube] Shows the youtube video indicated by id
sub [sub]Some text[/sub] Subscript text appears half a character below the normal line
sup [sup]Some text[/sup] Subscript text appears half a character below the normal line
small [small]Some text[/small] Makes text smaller
email [email]email@example.com[/email] Create a mailto bbcode
named-email [email=email@example.com]Send email[/email] Create a mailto bbcode

License

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