用 PHP 解析 vBulletin 的 BB 代码

发布于 2024-09-04 13:51:31 字数 258 浏览 5 评论 0原文

我想要一个将 vBulletin 中的 BB 代码解析为标准 HTML 标记的函数。

不使用 PEAR 库或 PECL 扩展,因为我不想在 PEAR 上大惊小怪,或者必须依赖于能够在此应用程序的每个实例上安装 PECL 扩展。目标是零依赖。如果我能找到 PEAR 扩展的源代码并对其进行修改就好了,但我似乎做不到。

具体来说,我遇到的问题是匹配[quote=My Name]。名称“我的名字”不包含任何内容,并且可以包含空格。

I would like a function that parses BB Code from vBulletin into a standard HTML markup.

Without using the PEAR library or the PECL extension, because I don't want to fuss with PEAR or have to depend on being able to install a PECL extension on every instance of this application. The goal is zero dependencies. It would be fine if I could find the source code for the PEAR extension and modify that, but I seem to be unable to.

Specifically the trouble I'm having is matching [quote=My Name]. The name 'My Name' isn't enclosed by anything and can contain spaces.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

近箐 2024-09-11 13:51:31

好吧,除了 PEAR 包PECL 扩展 您还有一个名为 Zend_Markup 非常易于使用(ZF 是松散耦合的,因此您可以选择仅使用该组件)。 PHP 类

Well, in addition to the PEAR package and the PECL extension you also have a Zend Framework Package called Zend_Markup which is very easy to use (ZF is loosely coupled, so you can choose to only use that component). There are also quite a few classes over at PHP Classes.

做个少女永远怀春 2024-09-11 13:51:31

一种方法是使用 http://www.christian -seiler.de/projekte/php/bbcode/index_en.html

该项目页面还包含类似工作的链接;即使您不喜欢其中之一,也可能会有用。

One way to do it would be to use http://www.christian-seiler.de/projekte/php/bbcode/index_en.html

The project page also contains links to similar efforts; one of them might be useful even if you don't like that one.

负佳期 2024-09-11 13:51:31

(简介:使用 vBulletin 开发了 5 年)

与大多数正则表达式或嵌入式库相比,vBulletin 的解析器相当复杂(可能是不必要的?)。老实说,我会仔细研究它,并尽你所能,因为他们做事的方式往往有点不同。如果您拥有一个完美工作的解析器,而不必查看它们本身如何实际生成/解析它,我会感到惊讶。

如果是旧数据,您可能只想编写自己的数据,但如果是旧数据和新数据传入,为什么不将其缓存在 vBulletin 端并使用它们生成的内容呢?或者直接使用 vB_BbCode_Parser 类...

希望这有帮助。

(intro: 5 years developing with vBulletin)

vBulletin's parser is pretty complex (possibly needlessly?) compared to most regular expressions out there or drop-in-libraries. I'd honestly just dig through it, and take out what you can, since they tend to do things a little differently. I'd be surprised if you got a perfectly working parser without having to see how they actually generate/parse it themselves.

If it's old data, you may want to just write your own, but if it's old and new data coming in, why not just have it cached on the vBulletin side and use what they generate? Or just use the vB_BbCode_Parser class directly...

Hope this helps.

乜一 2024-09-11 13:51:31

我建议你只调整 PEAR 扩展。它不依赖于其他 PEAR 库,因此应该相当简单。

I suggest you just adapt the PEAR extension. It has no dependencies on other PEAR libraries, so it should be fairly straightforward.

一刻暧昧 2024-09-11 13:51:31

最难的方法之一是使用正则表达式:

$text = "[quote=my name]something.
[b]bla[b]
blabla.[/quote]";
$search = "/\[quote=(?>([a-z0-9]*))\](.*)\[/quote\]/is";
$replace = "From <i>$1</i>: <q>$2</q>";
$return_text = preg_replace($search, $replace, $text);

echo nl2br($return_text);

/*
From <i>my name</i>: <q>something.
[b]bla[b]
blabla.</q>
*/

我说最难只是因为在“(.*)”上有可能发生灾难性的回溯,尤其是对于较长的文本,以及一些匹配项可能会漏掉的可能性。因此,您可能想直接访问源代码以找到正确的正则表达式: http://www.bbcode .org/implementations.php(请参阅:使用 PHP 的简单和复杂 BBCode 了解正则表达式,phpBBCode 了解源代码)。或者,您可以在此处复制并构建 Pear 的解析器源代码: http://svn.php .net/viewvc/pear/packages/HTML_BBCodeParser

One of the hardest ways is using regex:

$text = "[quote=my name]something.
[b]bla[b]
blabla.[/quote]";
$search = "/\[quote=(?>([a-z0-9]*))\](.*)\[/quote\]/is";
$replace = "From <i>$1</i>: <q>$2</q>";
$return_text = preg_replace($search, $replace, $text);

echo nl2br($return_text);

/*
From <i>my name</i>: <q>something.
[b]bla[b]
blabla.</q>
*/

I say hardest only because of the chance of catastrophic backtracking on that '(.*)', especially with longer text, and a chance of some matches slipping through the cracks. So, you may want to go straight to the source to find the proper regex: http://www.bbcode.org/implementations.php (see: Simple and Complex BBCode with PHP for regex, phpBBCode for sourcecode). Alternatively, you can duplicate and build upon Pear's parser sourcecode here: http://svn.php.net/viewvc/pear/packages/HTML_BBCodeParser

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文