php - 解析PHPBB3的BB代码

标签 php bbcode phpbb3

希望使用 PHPBB3 的函数在 PHP 中解析 BB 代码。我已经走到这一步了:

<?php
    include_once("../../forum/includes/functions_content.php");

    $text = "[b]bold text here[/b] not bold here";
    $uid = $bitfield = $options = '';

    echo("parsing");
    echo generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true);
    echo("finished");
?>

但是它回显 parsing 但之后不会继续。我期望输出是这样的:

<b>bold text here</b> not bold here

非常感谢任何帮助!

编辑

没有答案仍然有效。我正在寻找一个 Standalone php 页面,它使用 PHPBB3 的 BBCode 解析器将给定的 BB 代码字符串转换为 HTML 字符串。

最佳答案

生成 bbcodes 是一个 2 步过程,你做第一步(第一次通过)

generate_text_for_storage 用于将 bbcode 存储在数据库中,它存储为 bbcode 因为您可以更改 bbcode 而无需重新解析旧消息。

您正在寻找的另一个功能是

generate_text_for_display

PHPBB 有一个 wiki 列出了这样的东西

https://wiki.phpbb.com/Tutorial.Parsing_text

https://wiki.phpbb.com/Generate_text_for_display

是您要查找的页面。

或者你可以直接使用 bbcode 类,代码也可以工作

$bbcode = new bbcode(base64_encode($bbcode_bitfield));
$bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']);
$post_text = smiley_text($post_text);
$post_text = censor_text($post_text);

你需要

include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

最后一个工作

2 函数方法的完整代码,带输出

<?php
ini_set('display_errors', 1);
define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
$phpEx = "php";
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('viewforum');


$text = "[b]bold text here[/b] not bold here";

$uid = $bitfield = $options = '';
echo("parsing");
echo generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true);
var_dump($text);
$text = generate_text_for_display($text, $uid, $bitfield, OPTION_FLAG_BBCODE );
var_dump($text);
echo("finished");

哪些输出

parsing
string '[b:1vhn6cjx]bold text here[/b:1vhn6cjx] not bold here' (length=53)
array (size=1)
  0 => int 1
string '<span style="font-weight: bold">bold text here</span> not bold here' (length=67)
finished

bb 代码转换是一个 2 步过程,使用户和发布者都可以灵活地自定义帖子的 View 。您将需要先使用第一个函数处理文本,然后再进行第二次处理以获取 html

关于php - 解析PHPBB3的BB代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21939777/

相关文章:

php - Codeigniter:将 form_input 插入 sql 表

PHP 从具有关系数据的数组创建多维数组

Java 正则表达式模式不匹配括号

java - 为什么我的 JavaCC 解析器不解析小于 2 个字符的标记?

php - quercus php 和 RegexpException : Delimiter A in regexp 'Array' must not be backslash or alphanumeric

mysql - 无法从 phpbb 连接到 docker 容器中的 mysql

PHPUnit 测试和 Doctrine,连接太多

php - 用户图像上传文件夹的chmod设置?

php - 在 PHP 中解析 vBulletin BBCode

PhpBB如何更改帖子网址