2017 © Pedro Peláez
 

library bbcode

php parser for bb code

image

potaka/bbcode

php parser for bb code

  • Friday, February 24, 2017
  • by po_taka
  • Repository
  • 1 Watchers
  • 4 Stars
  • 1,115 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 47 % Grown

The README.md

BB code parser for php 7.0+

Builds:, (*1)

SensioLabsInsight, (*2)

Build Status, (*3)

There are two parts - tokenizer and parser., (*4)

Tokenization - convert string to tokens., (*5)

BbCodeParser - convert bbCodeTokens to html (+ some validations), (*6)

The Easy Way

$tokenizer = new \Potaka\BbCode\Tokenizer\Tokenizer();

$bbText = '[b]bold[/b]text[u]under[i]line[/i][/u]';

$tokenized = $tokenizer->tokenize($text);
$factory = new  \Potaka\BbCode\Factory();
$bbcode = $factory->getFullBbCode();
$html = $bbcode->format($tokenized);

The value if html is, (*7)

<b>bold</b>text<u>under<i>line</i></u>

Installation

composer require potaka/bbcode

Internal explanation

Tokenization

For example, (*8)

$bbText = '[b]bold[/b]text[u]under[i]line[/i][/u]';

Will be tokenized to, (*9)

Tag:
  type: null,
  tags:
    tag1:
      type: b
      tags:
        tag1:
          type: null
          text: bold
    tag2:
      type: null
      text: text
    tag3:
      type: u
      tags:
        tag1:
          type: null,
          text: under
        tag2:
          type: i
          tags:
            tag1:
              type: null,
              text: line

Tokenized to html

You need to have valid bb code tags. Build in tags are available in https://github.com/angelk/bbCode/tree/master/src/Potaka/BbCode/Tag, (*10)

Building the parser:, (*11)

use Potaka\BbCode;
use Potaka\BbCode\Tokenizer;

use Potaka\BbCode\Tag\Bold;
use Potaka\BbCode\Tag\Underline;
use Potaka\BbCode\Tag\Italic;

use Potaka\BbCode\Tag\Link;

$bbcode = new BbCode();

Lets add the b code, (*12)

$bold = new Bold();
$bbcode->addTag($bold);

Lets format the token from above, (*13)

$tokenizer = new Tokenizer();
$tokenized = $tokenizer->tokenize($bbText);

$bbcode->format($tokenized);

will return, (*14)

<b>bold</b>text[u]under[i]line[/i][/u]

u and i are not formated cuz tags are not added., (*15)

Lets add em., (*16)

$underline = new Underline();
$bbcode->addTag($underline);

$italic = new Italic();
$bbcode->addTag($italic);

Test again, (*17)

$bbcode->format($tokenized);

Result:, (*18)

<b>bold</b>text<u>under[i]line[/i]</u>

Why i is not converted? Cuz u doesn't allow child tag of type i. Lets fix this, (*19)

$bbcode->addAllowedChildTag($underline, $italic);

Everything should work now!, (*20)

Whats the purpose of this allowing? Imagine you have link [url]http://google.bg[/url]. What if someone try to put link in link [url=http://google.bg]google.[url=http://gmail.com]bg[/url][/url]? This will generate, (*21)

<a href="http://google.bg">
  google.<a href="http://gmail.com">bg</a>
</a>

This html is invalid. It could even provide xss. This is why you should not allow url inside url., (*22)

The Versions

24/02 2017

dev-master

9999999-dev

php parser for bb code

  Sources   Download

MIT

The Requires

 

The Development Requires

24/02 2017

0.2.1

0.2.1.0

php parser for bb code

  Sources   Download

MIT

The Requires

 

The Development Requires

02/02 2017

0.2.0

0.2.0.0

php parser for bb code

  Sources   Download

MIT

The Requires

 

The Development Requires

16/01 2017

0.1.5

0.1.5.0

php parser for bb code

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

15/01 2017

0.1.4

0.1.4.0

php parser for bb code

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

15/01 2017

0.1.3

0.1.3.0

php parser for bb code

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

15/01 2017

0.1.2

0.1.2.0

php parser for bb code

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

10/01 2017

0.1.1

0.1.1.0

php parser for bb code

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

17/12 2016

0.1.0

0.1.0.0

php parser for bb code

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires