BBCode to HTML conversion, in PHP7.
This is a PHP include that can convert BBCode into safe HTML output.
Features:
- UTF-8 aware
- Replaces unsafe HTML characters (
<
,>
,&
,<
,>
,&
,
) - Properly handles various bad input conditions, to ensure correcly closed tags and avoid malformed HTML
- Public-domain (The Unlicense), just copy-paste into your own projects!
These BBCode tags are supported and converted to HTML equivalent:
[b]
,[i]
,[u]
,[s]
,[sub]
,[sup]
[color=X]
,[size=Y]
,[font color=X size=Y]
[quote]
/[blockquote]
[pre]
/[code]
[ol]
,[ul]
,[li]
/[*]
[table]
,[tr]
,[th]
,[td]
[img]http://www.example.com/image.jpg[/img]
[url]http://www.example.com[/url]
/[url=http://www.example.com]Example URL[/url]
In addition, unsafe HTML characters are converted to safe HTML entities. Newlines are collapsed and translated as follows:
\r
characters are stripped- Single
\n
: converted to\n<br>
- Two or more sequential
\n
: converted to\n\n<p>
The resulting string is whitespace trimmed at beginning and end.
The common use case [quote author=X]
is deliberately excluded, because formatting the result for presentation is implementation-dependent.
bbcode.php is the include file. Place it in the folder of your choice and then include_once 'bbcode.php'
;
test.php is a unit test file. You do not need this when deploying.
Open an issue if you find a bug. Send a pull request if you want to extend it. I especially like more unit tests, better attempts at catching malformed tags, performance and optimization features.