ammarkannas / laravel-bbcode-parser
Parse your bbcode easy with this library.
This package's canonical repository appears to be gone and the package has been frozen as a result. Email us for help if needed.
Package info
github.com/ammarkannas/laravel-bbcode-parser
pkg:composer/ammarkannas/laravel-bbcode-parser
v1.0.1
2023-01-04 09:34 UTC
Requires
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
What is BBCode
BBCode on wikipedia
How does it work?
This package parse bbcode tags to html.
Install
Via Composer
composer require ammarkannas/laravel-bbcode-parser
Usage With Laravel
To parse some text it's as easy as this!
use Ammar\BBCode\Facades\BBCode; echo BBCode::parse('[b]Text![/b]'); // The result is '<strong>Text!</strong>'
Parse only selected tags.
echo BBCode::only(['bold', 'italic']) ->parse('[b][u]text[/u] [i]text[/i]![/b]'); /** * <strong> * [u]Text[/u] * <span style="font-style: italic;">text</span> * </strong> */ echo BBCode::only('bold', 'italic') ->parse('[b][u]text[/u] [i]text[/i]![/b]');
Parse all except one or more tags.
echo BBCode::except('bold') ->parse('[b]text[/b] [i]text[/i]'); /** * [b]text[/b] * <span style="font-style: italic;">text</span> */
Case sensitive & insensitive
By default, the parser is case sensitive.
# Case insensitive echo BBCode::parse('[b]Bold[/b] [I]Italic![/I]', true); # or other way echo BBCode::parseCaseInsensitive('[b]Bold[/b] [i]Italic[/i]');
Strip or remove all bbcode tags
BBCode::stripBBCodeTags('[b]Bold[/b] [i]Italic![/i]');
Laravel Blade
@bb('[b]Bold[/b] [i]Italic[/i]') {{-- <strong>Bold</strong> <em>Italic</em> --}} @bbexcept('bold', '[b]Bold[/b] [i]Italic[/i]') {{-- [b]Bold[/b] <em>Italic</em> --}} @bbonly('bold', '[b]Bold[/b] [i]Italic[/i]') {{-- <strong>Bold</strong> [i]Italic[/i] --}}
Extending or editing BBCode tags
Can add custom bbcode tags inside config file
php artisan vendor:publish --provider="Rwxrwx\BBCode\BBCodeServiceProvider" --tag="bbcodes-config"
Or you can add using method
<?php namespace App\Providers; use Ammar\BBCode\Facades\BBCode; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { BBCode::addTag( name: 'size', // $1 $2 search: '/\[size\=([1-7])\](.*?)\[\/size\]/s', replace: '<span style="font-size: $1px;">$2</span>', content: '$2' // content param ); } }
Using
BBCode::parse('[size=2]text[/size] [b]Example[/b]'); BBCode::except('size')->parse('[size=2]text[/size] [b]Example[/b]'); BBCode::only('size')->parse('[size=2]text[/size] [b]Example[/b]');
License
The MIT License (MIT). Please see License File for more information.