Skip to content

Commit b2b8312

Browse files
authored
Merge pull request #1 from igorakaamigo/basic-implementation
Basic implementation
2 parents d69f7d0 + 53e61ae commit b2b8312

6 files changed

Lines changed: 396 additions & 7 deletions

File tree

.gitignore

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
composer.phar
21
/vendor/
3-
4-
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
5-
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
6-
# composer.lock
2+
composer.lock
3+
composer.phar
4+
.DS_Store
5+
Thumbs.db

README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
1-
# php5-tiny-bbcode
2-
A tiny BBCode implementation for old PHP5
1+
# Tiny BBCode implementation for PHP 5.4+
2+
3+
This library includes a lightweight implementation of a BBCode subset to HTML translator.
4+
5+
## Features
6+
7+
* It's tiny, yep.
8+
* PSR-4 autoloading compliant structure
9+
* Unit-Testing with PHPUnit
10+
* Easy to use to any framework or even a plain php file
11+
12+
## Installation
13+
14+
The suggested installation method is via [composer](https://getcomposer.org/):
15+
16+
```sh
17+
php composer.phar require "igorakaamigo/php5-tiny-bbcode"
18+
```
19+
20+
## Usage
21+
22+
```php
23+
use \Igorakaamigo\Utils\BBCode;
24+
25+
echo BBCode::convert('[b]A bold string[/b]');
26+
```
27+
28+
### Supported BBCodes
29+
30+
* [b]Bold string[/b]
31+
* [i]Italic string[/i]
32+
* [u]Underline string[/u]
33+
* [s]Strikethrough string[/s]
34+
* [url]http://www.domain.tld[/url]
35+
* [url=http://www.domain.tld]Another way to render a link[/url]
36+
* [img]http://www.domain.tld/upload/image.png[/img]
37+
* [quote]A quotation[/quote]
38+
* [code]A program code sample[/code]
39+
* [size=12]A text written using a 12px-sized font[/size]
40+
* [size="10pt"]A text written using a 10pt-sized font[/size]
41+
* [color="#33FF33"]A green text line[/color]
42+
* [ul], [ol], [li] – list-related tags
43+
* [table], [tr], [td] – table-related tags
44+
45+
## Contributing
46+
47+
OMG! Really? Thanks a lot!
48+
49+
Fork --> modify --> pull-request

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "igorakaamigo/php5-tiny-bbcode",
3+
"description": "A tiny BBCode implementation for old PHP5.",
4+
"keywords": [
5+
"php5",
6+
"library",
7+
"utils",
8+
"bbcode"
9+
],
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Igor M Osipov",
14+
"email": "osipov.igor.amigo@gmail.com"
15+
}
16+
],
17+
"type": "library",
18+
"require": {
19+
"php": ">=5.4"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "5.4.*"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"Igorakaamigo\\Utils\\": "src/"
27+
}
28+
},
29+
"scripts": {
30+
"test": "phpunit"
31+
}
32+
}

phpunit.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<phpunit bootstrap="vendor/autoload.php"
2+
colors="true"
3+
convertErrorsToExceptions="true"
4+
convertNoticesToExceptions="true"
5+
convertWarningsToExceptions="true"
6+
stopOnFailure="true">
7+
<testsuites>
8+
<testsuite name="Unit Tests">
9+
<directory>tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
</phpunit>

src/BBCode.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2017 Igor M Osipov
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
namespace Igorakaamigo\Utils;
27+
28+
/**
29+
* Class BBCode
30+
*
31+
* A lightweight BBCode subset implementation.
32+
*
33+
* @package Igorakaamigo\Utils
34+
*/
35+
class BBCode
36+
{
37+
private static $_patterns = array(
38+
'#\[b\](.*?)\[/b\]#si' => '<strong>\1</strong>',
39+
'#\[i\](.*?)\[/i\]#si' => '<em>\1</em>',
40+
'#\[u\](.*?)\[/u\]#si' => '<span style="text-decoration: underline;">\1</span>',
41+
'#\[s\](.*?)\[/s\]#si' => '<del>\1</del>',
42+
'#\[url\](.*?)\[/url\]#si' => '<a href="\1">\1</a>',
43+
'#\[url=(.*?)\](.*?)\[/url\]#si' => '<a href="\1">\2</a>',
44+
'#\[img\](.*?)\[/img\]#si' => '<img src="\1" alt="" />',
45+
'#\[quote(=&quot;.*?&quot;)?\](.*?)\[/quote\]#si' => '<blockquote>\2</blockquote>',
46+
'#\[code\](.*?)\[/code\]#si' => '<code style="white-space: pre;">\1</code>',
47+
'#\[size=(\d+)\](.*?)\[/size\]#si' => '<span style="font-size: \1px;">\2</span>',
48+
'#\[size=&quot;(.*?)&quot;\](.*?)\[/size\]#si' => '<span style="font-size: \1;">\2</span>',
49+
'#\[color=&quot;(.*?)&quot;\](.*?)\[/color\]#si' => '<span style="color: \1;">\2</span>',
50+
'#\[li\](.*?)\[/li\]#si' => '<li>\1</li>',
51+
'#\[ul\](.*?)\[/ul\]#si' => '<ul>\1</ul>',
52+
'#\[ol\](.*?)\[/ol\]#si' => '<ol>\1</ol>',
53+
'#\[table\](.*?)\[/table\]#si' => '<table>\1</table>',
54+
'#\[tr\](.*?)\[/tr\]#si' => '<tr>\1</tr>',
55+
'#\[td\](.*?)\[/td\]#si' => '<td>\1</td>',
56+
'#(^\s+)|(\s+$)#si' => '',
57+
);
58+
59+
/**
60+
* BBCode translation
61+
*
62+
* @param string $sourceString A string containing BBCode tags
63+
* @return string HTML string
64+
*/
65+
static function convert($sourceString)
66+
{
67+
return preg_replace(
68+
array_keys(self::$_patterns),
69+
array_values(self::$_patterns),
70+
htmlentities($sourceString)
71+
);
72+
}
73+
}

0 commit comments

Comments
 (0)