- Notifications
You must be signed in to change notification settings - Fork27
BBCode parser with bidirectional conversion.
License
genert/bbcode
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
BBCode parser from or to HTML.
PHP 7.1+ is required.
To get the latest version of BBCode, simply require the project usingComposer:
$ composer require genert/bbcode
Convert BBCode to HTML and returns parsed text as string.
Example:
useGenert\BBCode\BBCode;$bbCode =newBBCode();// Output: '[b]Hello word![/b]'$bbCode->convertFromHtml('<strong>Hello word!</strong>');
Convert HTML to BBCode and returns parsed text as string.
Example:
useGenert\BBCode\BBCode;$bbCode =newBBCode();// Output: '<strong>Hello word!</strong>'$bbCode->convertToHtml('[b]Hello word![/b]');
This function also supports case sensitive BBCode parsing by optional parameter.
To enable this, simply passBBCode::CASE_SENSITIVE
as second argument:
// Output: '<strong><i><u>Ran<strong>d</strong>om text</u></i></strong>'$bbCode->convertToHtml('[B][I][U]Ran[b]d[/b]om text[/u][/I][/b]', BBCode::CASE_SENSITIVE);
Strips BBCode tags from text and returns output as string.
Example:
useGenert\BBCode\BBCode;$bbCode =newBBCode();// Output: 'Hello word!'$bbCode->stripBBCodeTags('[b]Hello word![/b]');
Sets parser to only convert set BBCode tags.
Example:
useGenert\BBCode\BBCode;$bbCode =newBBCode();// Output: '<strong>Bold</strong> [i]italic[/i]'$bbCode->only('bold')->convertToHtml('[b]Bold[/b] [i]italic[/i]');// Or as array$bbCode->only(['bold'])->convertToHtml('[b]Bold[/b] [i]italic[/i]');
Sets parser to only convert all BBCode tags except listed.
Example:
useGenert\BBCode\BBCode;$bbCode =newBBCode();// Output: '[b]Bold[/b] <i>italic</i>'$bbCode->except('bold')->convertToHtml('[b]Bold[/b] [i]italic[/i]');// Or as array$bbCode->except(['bold'])->convertToHtml('[b]Bold[/b] [i]italic[/i]');
Add regex based BBCode parser to translate found pattern to desired one.
Example:
useGenert\BBCode\BBCode;$bbCode =newBBCode();// Add "[link target=http://example.com]Example[/link]" parser.$bbCode->addParser('custom-link','/\[link target\=(.*?)\](.*?)\[\/link\]/s','<a href="$1">$2</a>','$1');// Output: '<a href="www.yourlinkhere.com">Text to be displayed</a>.'$bbCode->convertToHtml('[link target=www.yourlinkhere.com]Text to be displayed[/link].');
Add HTML parser to translate pattern to desired one.
SeeaddParser
for example code.
Adds linebreak parser to BBCode parsers list to convert newlines to<br />
in HTML.
Once BBCode is installed, you need to register the service provider. Open upconfig/app.php
and add the following to theproviders
key.
\Genert\BBCode\BBCodeServiceProvider::class,
You can register facades in thealiases
key of yourconfig/app.php
file if you like.
'BBCode' => \Genert\BBCode\Facades\BBCode::class,
With registered facade, you can use library's functionality as following:
// Output: '<strong>Laravel wins</strong>'echo BBCode::convertToHtml('[b]Laravel wins[/b]');// Output: '[b]Do Symphony or not[/b]'echo BBCode::convertFromHtml('<strong>Do Symphony or not</strong>');// Output: '<strong>What does<strong> [i]fox say[/i]'echo BBCode::only('bold')->convertToHtml('[b]What does[/b] [i]fox say[/i]');
To run tests, simply run following command in terminal:
composertest
Contributions are welcome. Please clearly explain the purpose of the PR and follow the current style.
Issues can be resolved quickest if they are descriptive and include both a reduced test case and a set of steps to reproduce.
Thegenert/bbcode
library is copyright ©Genert Org and licensed for use under the MIT License (MIT).
Please seeMIT License for more information.
About
BBCode parser with bidirectional conversion.