Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
composer.phar
/vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
composer.lock
composer.phar
.DS_Store
Thumbs.db
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
# php5-tiny-bbcode
A tiny BBCode implementation for old PHP5
# Tiny BBCode implementation for PHP 5.4+

This library includes a lightweight implementation of a BBCode subset to HTML translator.

## Features

* It's tiny, yep.
* PSR-4 autoloading compliant structure
* Unit-Testing with PHPUnit
* Easy to use to any framework or even a plain php file

## Installation

The suggested installation method is via [composer](https://getcomposer.org/):

```sh
php composer.phar require "igorakaamigo/php5-tiny-bbcode"
```

## Usage

```php
use \Igorakaamigo\Utils\BBCode;

echo BBCode::convert('[b]A bold string[/b]');
```

### Supported BBCodes

* [b]Bold string[/b]
* [i]Italic string[/i]
* [u]Underline string[/u]
* [s]Strikethrough string[/s]
* [url]http://www.domain.tld[/url]
* [url=http://www.domain.tld]Another way to render a link[/url]
* [img]http://www.domain.tld/upload/image.png[/img]
* [quote]A quotation[/quote]
* [code]A program code sample[/code]
* [size=12]A text written using a 12px-sized font[/size]
* [size="10pt"]A text written using a 10pt-sized font[/size]
* [color="#33FF33"]A green text line[/color]
* [ul], [ol], [li] – list-related tags
* [table], [tr], [td] – table-related tags

## Contributing

OMG! Really? Thanks a lot!

Fork --> modify --> pull-request
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "igorakaamigo/php5-tiny-bbcode",
"description": "A tiny BBCode implementation for old PHP5.",
"keywords": [
"php5",
"library",
"utils",
"bbcode"
],
"license": "MIT",
"authors": [
{
"name": "Igor M Osipov",
"email": "osipov.igor.amigo@gmail.com"
}
],
"type": "library",
"require": {
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "5.4.*"
},
"autoload": {
"psr-4": {
"Igorakaamigo\\Utils\\": "src/"
}
},
"scripts": {
"test": "phpunit"
}
}
12 changes: 12 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<phpunit bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true">
<testsuites>
<testsuite name="Unit Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
73 changes: 73 additions & 0 deletions src/BBCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* MIT License
*
* Copyright (c) 2017 Igor M Osipov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace Igorakaamigo\Utils;

/**
* Class BBCode
*
* A lightweight BBCode subset implementation.
*
* @package Igorakaamigo\Utils
*/
class BBCode
{
private static $_patterns = array(
'#\[b\](.*?)\[/b\]#si' => '<strong>\1</strong>',
'#\[i\](.*?)\[/i\]#si' => '<em>\1</em>',
'#\[u\](.*?)\[/u\]#si' => '<span style="text-decoration: underline;">\1</span>',
'#\[s\](.*?)\[/s\]#si' => '<del>\1</del>',
'#\[url\](.*?)\[/url\]#si' => '<a href="\1">\1</a>',
'#\[url=(.*?)\](.*?)\[/url\]#si' => '<a href="\1">\2</a>',
'#\[img\](.*?)\[/img\]#si' => '<img src="\1" alt="" />',
'#\[quote(=&quot;.*?&quot;)?\](.*?)\[/quote\]#si' => '<blockquote>\2</blockquote>',
'#\[code\](.*?)\[/code\]#si' => '<code style="white-space: pre;">\1</code>',
'#\[size=(\d+)\](.*?)\[/size\]#si' => '<span style="font-size: \1px;">\2</span>',
'#\[size=&quot;(.*?)&quot;\](.*?)\[/size\]#si' => '<span style="font-size: \1;">\2</span>',
'#\[color=&quot;(.*?)&quot;\](.*?)\[/color\]#si' => '<span style="color: \1;">\2</span>',
'#\[li\](.*?)\[/li\]#si' => '<li>\1</li>',
'#\[ul\](.*?)\[/ul\]#si' => '<ul>\1</ul>',
'#\[ol\](.*?)\[/ol\]#si' => '<ol>\1</ol>',
'#\[table\](.*?)\[/table\]#si' => '<table>\1</table>',
'#\[tr\](.*?)\[/tr\]#si' => '<tr>\1</tr>',
'#\[td\](.*?)\[/td\]#si' => '<td>\1</td>',
'#(^\s+)|(\s+$)#si' => '',
);

/**
* BBCode translation
*
* @param string $sourceString A string containing BBCode tags
* @return string HTML string
*/
static function convert($sourceString)
{
return preg_replace(
array_keys(self::$_patterns),
array_values(self::$_patterns),
htmlentities($sourceString)
);
}
}
Loading