Looking to hire Laravel developers? Try LaraJobs

laravel-bbcode-parser-zaym maintained by daddyfrosty

Description
Parse your bbcode easy with this library.
Last update
2023/05/29 23:58 (dev-main)
License
Links
Downloads
10
Tags

Comments
comments powered by Disqus

Laravel BBCode Parser

What is BBCode

BBCode on wikipedia

How does it work?

This package parse bbcode tags to html.

Install

Via Composer

composer require daddyfrosty/laravel-bbcode-parser-zaym

Usage With Laravel

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

use 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 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.