PHP code example of genert / bbcode

1. Go to this page and download the library: Download genert/bbcode library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

genert / bbcode example snippets


use Genert\BBCode\BBCode;

$bbCode = new BBCode();

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

use Genert\BBCode\BBCode;

$bbCode = new BBCode();

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

// 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);

use Genert\BBCode\BBCode;

$bbCode = new BBCode();

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

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]');

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]');

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].');

// 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]');