chriskonnertz / bbcode

A BBCode parser and converter written in PHP.
MIT License
40 stars 25 forks source link
bbcode converter html parser php

BBCode

Build Status GitHub license Version

A library that parses BBCode and converts it to HTML code. Written in PHP.

Installation

Through Composer:

composer require chriskonnertz/bbcode

From then on you may run composer update to get the latest version of this library.

It is possible to use this library without using Composer but then it is necessary to register an autoloader function.

This library requires PHP 5.5 or higher.

Usage example

Here is a minimalistic example of PHP code that uses this library. It assumes that there is an autoloader.

$bbcode = new ChrisKonnertz\BBCode\BBCode();

$rendered = $bbcode->render('[b]Hello world![/b]');

echo $rendered;

Available tags

You can add custom tags with the addTag($name, Closure $closure) method.

Helpful methods

The BBCode class also implements the __toString() method, which internally calls the render() method.

Custom tag example

This code will add a [h1] BBCode tag that creates a <h1> HTML tag:

$bbcode->addTag('h1', function($tag, &$html, $openingTag) {
    if ($tag->opening) {
        return '<h1>';
    } else {
        return '</h1>';
    }
});

Multibyte characters

This library supports the use of multibyte characters.

Fork

This repository originally has been forked from kaimallea/php-bbcode. However, it has been completely rewritten since then.

Status

Status of this repository: Maintained. Create an issue and you will get a response, usually within 48 hours.