genert/bbcode
Composer 安装命令:
composer require genert/bbcode
包简介
BBCode parser from or to HTML.
README 文档
README
BBCode parser from or to HTML.
Installation
PHP 7.1+ is required.
To get the latest version of BBCode, simply require the project using Composer:
$ composer require genert/bbcode
Usage
convertFromHtml(string $text)
Convert BBCode to HTML and returns parsed text as string.
Example:
use Genert\BBCode\BBCode; $bbCode = new BBCode(); // Output: '[b]Hello word![/b]' $bbCode->convertFromHtml('<strong>Hello word!</strong>');
convertToHtml(string $text, [$caseSensitive])
Convert HTML to BBCode and returns parsed text as string.
Example:
use Genert\BBCode\BBCode; $bbCode = new BBCode(); // 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 pass BBCode::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);
stripBBCodeTags(string $text)
Strips BBCode tags from text and returns output as string.
Example:
use Genert\BBCode\BBCode; $bbCode = new BBCode(); // Output: 'Hello word!' $bbCode->stripBBCodeTags('[b]Hello word![/b]');
only(array list or ...args)
Sets parser to only convert set BBCode tags.
Example:
use Genert\BBCode\BBCode; $bbCode = new BBCode(); // 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]');
except(array list or ...args)
Sets parser to only convert all BBCode tags except listed.
Example:
use Genert\BBCode\BBCode; $bbCode = new BBCode(); // 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]');
addParser(string $name, string $pattern, string $replace, string $content)
Add regex based BBCode parser to translate found pattern to desired one.
Example:
use Genert\BBCode\BBCode; $bbCode = new BBCode(); // 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].');
addHtmlParser(string $name, string $pattern, string $replace, string $content)
Add HTML parser to translate pattern to desired one.
See addParser for example code.
addLinebreakParser()
Adds linebreak parser to BBCode parsers list to convert newlines to <br /> in HTML.
Laravel installation
Once BBCode is installed, you need to register the service provider. Open up config/app.php and add the following to the providers key.
\Genert\BBCode\BBCodeServiceProvider::class,
You can register facades in the aliases key of your config/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]');
Testing
To run tests, simply run following command in terminal:
composer test
Contributions & Issues
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.
Licence
The genert/bbcode library is copyright © Genert Org and licensed for use under the MIT License (MIT).
Please see MIT License for more information.
genert/bbcode 适用场景与选型建议
genert/bbcode 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 333.91k 次下载、GitHub Stars 达 80, 最近一次更新时间为 2017 年 08 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「laravel」 「bbcode」 「PSR-2」 「PSR-1」 「PSR-4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 genert/bbcode 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 genert/bbcode 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 genert/bbcode 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony FMBbCodeBundle
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Parse your BBCode easy with this library.
BBCode parser
An MT940 bank statement parser for PHP
Adds more BBCode
统计信息
- 总下载量: 333.91k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 81
- 点击次数: 30
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-10