ermarian/xbbcode

一个可扩展的BBCode解析工具。

1.1.0 2017-07-19 16:11 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:47:05 UTC


README

这是一个BBCode标记语言的解析器,支持每个标签的任意渲染插件。

安装

如下将库添加到您的composer依赖项中

composer require ermarian/xbbcode

使用

解析器可以按照以下方式调用

<?php

use \Ermarian\XBBCode\Processor\CallbackTagProcessor;
use \Ermarian\XBBCode\Processor\TemplateTagProcessor;
use \Ermarian\XBBCode\Tree\TagElementInterface;
use \Ermarian\XBBCode\XBBCodeParser;

$parser = new XBBCodeParser([
  'b' => new TemplateTagProcessor('<strong>{content}</content>'),
  'url' => new CallbackTagProcessor(function(TagElementInterface $tag) {
    $url = htmlspecialchars($tag->getOption());
    return '<a href="' . $url . '>' . $tag->getContent() . '</a>';
  }),
]);

print $parser->parse('[b]Hello [url=http://example.com]world[/url]![/b]')->render();

?>
<strong>Hello <a href="http://example.com">world</a>!</strong>

更强大的处理器插件可以简单地扩展TagProcessorBase并实现doProcess来执行自己的渲染。

语法

这里使用的BBCode的语法如下

text = { VCHAR / LWSP / element1 /.../ elementN }

其中每个elementN采用以下形式(对于包含仅小写字母数字字符和下划线的特定值$name

elementN = "[$name" argument "]" text "[/$name]"
argument = option / { WSP attribute }
option = "=" option-value
attribute = name "=" attribute-value
name = { ALPHA | DIGIT | "_" }

option-valueattribute-value字符串必须用引号引起来或使用反斜杠转义终止分隔符(空格和])。

标签必须正确嵌套,否则将被跳过。例如,在输入[b][i][/b][/i]中,只有[b]标签将被解析。