Tom Gullen Tom Gullen - 2 months ago 7
PHP Question

Parse BB code for PHPBB3

Looking to parse BB code in PHP using PHPBB3's functions. I've got this far:

<?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");
?>


However it echos
parsing
but doesn't continue after that. I'm expecting the output to be along the lines of:

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


Any help greatly appreciated!

Edit

No answers work still. I'm looking for a Standalone php page that turns given BB code string into an HTML string using PHPBB3's BBCode parser.

Answer

Generating the bbcodes is a 2 step processes, Your doing the first step (first pass)

generate_text_for_storage is made for storing the bbcode in the database, Its stored as bbcode because you can change the bbcode without the need for reparsing old messages.

The other function your looking for is

generate_text_for_display

PHPBB has a wiki listing things like this

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

https://wiki.phpbb.com/Generate_text_for_display

are the pages your looking for.

Alternatively you can use the bbcode class direct, code which also works

$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);

You will need

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

for the last one to work

Full code for the 2 functions method, with output

<?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");

Which outputs

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 code conversion is a 2 step process to give the flexibility for both user and poster to customize the view of the post. You will need to process the text first with the first function and then process a second time to get the html