2017 © Pedro Peláez
 

library bbcode

BBCode parser from or to HTML.

image

genert/bbcode

BBCode parser from or to HTML.

  • Sunday, March 4, 2018
  • by Genert
  • Repository
  • 3 Watchers
  • 17 Stars
  • 5,289 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 9 Forks
  • 8 Open issues
  • 6 Versions
  • 60 % Grown

The README.md

BBCode

Latest Version Software License Build Status, (*1)

BBCode parser from or to HTML., (*2)

Installation

PHP 7.1+ is required., (*3)

To get the latest version of BBCode, simply require the project using Composer:, (*4)

$ composer require genert/bbcode

Usage

convertFromHtml(string $text)

Convert BBCode to HTML and returns parsed text as string., (*5)

Example:, (*6)

use Genert\BBCode\BBCode;

$bbCode = new BBCode();

// Output: '[b]Hello word![/b]'
$bbCode->convertFromHtml('<strong>Hello word!</strong>');

convertToHtml(string $text, [$caseSensitive])

Convert HTML to BBCode and returns parsed text as string., (*7)

Example:, (*8)

use Genert\BBCode\BBCode;

$bbCode = new BBCode();

// Output: '<strong>Hello word!</strong>'
$bbCode->convertToHtml('[b]Hello word![/b]');

This function also supports case sensitive BBCode parsing by optional parameter., (*9)

To enable this, simply pass BBCode::CASE_SENSITIVE as second argument:, (*10)

// Output: '<strong><i><u>Ran<strong>d</strong>om text</u></i></strong>'
$bbCode->convertToHtml('[B][I][U]Ran[b]d[/b]om text[/u][/I][/b]', BBCode::CASE_SENSITIVE);

stripBBCodeTags(string $text)

Strips BBCode tags from text and returns output as string., (*11)

Example:, (*12)

use Genert\BBCode\BBCode;

$bbCode = new BBCode();

// Output: 'Hello word!'
$bbCode->stripBBCodeTags('[b]Hello word![/b]');

only(array list or ...args)

Sets parser to only convert set BBCode tags., (*13)

Example:, (*14)

use Genert\BBCode\BBCode;

$bbCode = new BBCode();

// Output: '<strong>Bold</strong> [i]italic[/i]'
$bbCode->only('bold')->convertToHtml('[b]Bold[/b] [i]italic[/i]');

// Or as array
$bbCode->only(['bold'])->convertToHtml('[b]Bold[/b] [i]italic[/i]');

except(array list or ...args)

Sets parser to only convert all BBCode tags except listed., (*15)

Example:, (*16)

use Genert\BBCode\BBCode;

$bbCode = new BBCode();

// Output: '[b]Bold[/b] <i>italic</i>'
$bbCode->except('bold')->convertToHtml('[b]Bold[/b] [i]italic[/i]');

// Or as array
$bbCode->except(['bold'])->convertToHtml('[b]Bold[/b] [i]italic[/i]');

addParser(string $name, string $pattern, string $replace, string $content)

Add regex based BBCode parser to translate found pattern to desired one., (*17)

Example:, (*18)

use Genert\BBCode\BBCode;

$bbCode = new BBCode();

// Add "[link target=http://example.com]Example[/link]" parser.
$bbCode->addParser(
    'custom-link',
    '/\[link target\=(.*?)\](.*?)\[\/link\]/s',
    '<a href="$1">$2</a>',
    '$1'
);

// Output: '<a href="www.yourlinkhere.com">Text to be displayed</a>.'
$bbCode->convertToHtml('[link target=www.yourlinkhere.com]Text to be displayed[/link].');

addHtmlParser(string $name, string $pattern, string $replace, string $content)

Add HTML parser to translate pattern to desired one., (*19)

See addParser for example code., (*20)

addLinebreakParser()

Adds linebreak parser to BBCode parsers list to convert newlines to <br /> in HTML., (*21)

Laravel installation

Once BBCode is installed, you need to register the service provider. Open up config/app.php and add the following to the providers key., (*22)

  • \Genert\BBCode\BBCodeServiceProvider::class,

You can register facades in the aliases key of your config/app.php file if you like., (*23)

  • 'BBCode' => \Genert\BBCode\Facades\BBCode::class,

With registered facade, you can use library's functionality as following:, (*24)

// Output: '<strong>Laravel wins</strong>'
echo BBCode::convertToHtml('[b]Laravel wins[/b]');

// Output: '[b]Do Symphony or not[/b]'
echo BBCode::convertFromHtml('<strong>Do Symphony or not</strong>');

// Output: '<strong>What does<strong> [i]fox say[/i]'
echo BBCode::only('bold')->convertToHtml('[b]What does[/b] [i]fox say[/i]');

Testing

To run tests, simply run following command in terminal:, (*25)

composer test

Contributions & Issues

Contributions are welcome. Please clearly explain the purpose of the PR and follow the current style., (*26)

Issues can be resolved quickest if they are descriptive and include both a reduced test case and a set of steps to reproduce., (*27)

Licence

The genert/bbcode library is copyright © Genert Org and licensed for use under the MIT License (MIT)., (*28)

Please see MIT License for more information., (*29)

The Versions

04/03 2018

dev-master

9999999-dev

BBCode parser from or to HTML.

  Sources   Download

MIT

The Development Requires

laravel parser bbcode psr-4 psr-2 psr-1

04/03 2018

1.1.0

1.1.0.0

BBCode parser from or to HTML.

  Sources   Download

MIT

The Development Requires

laravel parser bbcode psr-4 psr-2 psr-1

18/07 2017

1.0.3

1.0.3.0

BBCode parser from or to HTML.

  Sources   Download

MIT

The Development Requires

laravel parser bbcode psr-4 psr-2 psr-1

18/07 2017

1.0.2

1.0.2.0

BBCode parser from or to HTML.

  Sources   Download

MIT

The Development Requires

laravel parser bbcode psr-4 psr-2 psr-1

17/07 2017

1.0.1

1.0.1.0

BBCode parser from or to HTML.

  Sources   Download

MIT

The Development Requires

laravel parser bbcode psr-4 psr-2 psr-1

14/07 2017

1.0.0

1.0.0.0

BBCode parser from or to HTML.

  Sources   Download

MIT

The Development Requires

laravel parser bbcode psr-4 psr-2 psr-1