How to use phpBB3 bbcode parser?

Discussion forum for MOD Writers regarding MOD Development.
CatfishJools
Registered User
Posts: 6
Joined: Thu Dec 27, 2007 3:32 pm

Re: How to use phpBB3 bbcode parser?

Post by CatfishJools »

All~

I've been at this since Christmas eve and I still can't figure it out! I'm trying to correctly set the bitfield value for a converted phpBB2 database which had a custom BBcode in it called [clog]. After conversion to phpBB3, these tags just display as [clog]whatever[/clog]. I thought I'd be clever and just write a one-off fixer script to work through the posts table and sort all this out, but I have not got it to work (surprise, surprise).

The code is as follows and was mostly created by following the useful discussion here. So...

Code: Select all

<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../phpBB3/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

$sql = "SELECT * FROM `phpbb3_posts` WHERE `post_text` LIKE '%[clog:%' LIMIT 0,30;";
$result = $db->sql_query($sql);

while ($row = $db->sql_fetchrow($result))
{
	$count++;
	$uid = $row["bbcode_uid"];
	$text = $row["post_text"];
	$bitfield = $options = '';
	$allow_bbcode = $allow_urls = $allow_smilies = true;
	generate_text_for_storage($text, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);	
	echo "<p><strong>Record:".$count." (" . $row["post_id"] . ")<br/>Bitfield:".$bitfield."<br/>UID:" . $uid . "</strong><br/>" . $text . "</p>";
	$update_sql = "UPDATE `phpbb3_posts` SET `post_text` = '" . $text . "', 
	                                         `bbcode_bitfield` = '" . $bitfield . "',
																					 `bbcode_uid` = '" . $uid . "'
																					  WHERE `post_id` =" . $row["post_id"] . " LIMIT 1 ;";
	//$db->sql_query($update_sql);											
}
$db->sql_freeresult($result);
?>
Sample output is as follows:

Code: Select all

Record:1 (28623)
Bitfield:
UID:
In the 10 days before the forum turns one year old on December 30th. That's the plan anyway... So to start things off, I'm announcing a nifty new BBCode!!! [center][b:1uhfqbbj][u:1uhfqbbj][size=167:1uhfqbbj][clog:1uhfqbbj][/size:1uhfqbbj][/u:1uhfqbbj][/b:1uhfqbbj][/center] What's a BBCode? Looking in the FAQ would be a start (except I just noticed that it's down). BBCodes are the little "tags" that you can use in your post to make it more interesting, eye catching, etc. What does this new one do? It slices, dices, italicizes and links species names, all in one fell swoop! Here's how to use it: [size=167:1uhfqbbj][code:1uhfqbbj][clog]Hypancistrus zebra[/clog][/code:1uhfqbbj][/size:1uhfqbbj] Would automatically spit out: [clog:1uhfqbbj]Hypancistrus zebra[/clog:1uhfqbbj] A live, clickable link without having to root through the huge Cat-eLog. Of course, spelling counts, and at present it is a bit a bit picky about spaces.
The output appears to be missing a bitfield and a UID value. So, can anyone suggest where I am going wrong?

Many thanks,

Jools
User avatar
A_Jelly_Doughnut
Former Team Member
Posts: 34459
Joined: Sat Jan 18, 2003 1:26 am
Location: Where the Rivers Run
Contact:

Re: How to use phpBB3 bbcode parser?

Post by A_Jelly_Doughnut »

I assume [clog] exists as a custom BBCode.

Code: Select all

$sql = "SELECT * FROM `phpbb3_posts` WHERE `post_text` LIKE '%[clog:%' LIMIT 0,30;";
If I would remove the : from the LIKE clause, like so:

Code: Select all

$sql = "SELECT * FROM `phpbb3_posts` WHERE `post_text` LIKE '%[clog%' LIMIT 0,30;";
(Since it doesn't seem that you're getting anything that has been previously parsed, the : should not be there; else I misunderstand the problem)
A Donut's Blog
"Bach's Prelude (Cello Suite No. 1) is driving Indiana country roads in Autumn" - Ann Kish
squingynaut
Registered User
Posts: 6
Joined: Thu Nov 25, 2004 3:03 am

Re: How to use phpBB3 bbcode parser?

Post by squingynaut »

I've been having a similar problem, maybe one of you guys can help me out... There was an old page I made for phpbb2 that pulled posts from the phpbb database and displayed it on a "news" page. Trying to upgrade it for a different purpose, but using the old page as a testing ground, I'm trying to get it to parse phpbb3 bbcode properly.

Here's the topic on the forum that I'm pulling from
Here's the old news page I'm testing with

As you can see, on some of the bbcodes on the news page, it's not putting the ending tags on there correctly...

The "news page" is actually embedded inside another php file inside of an html table, so I can't use sessions in this project. To make up for that, I copy/pasted my bbcode.php file into my forum's main directory so I could include it with a direct link to the bbcode.html I'm using to avoid the "/board/styles//template/bbcode.html is missing" problem (that's what the my_phpbb.php is). Now that that's explained, here's my bit of code:

Code: Select all

 define('IN_PHPBB', true);
 $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
 $phpEx = substr(strrchr(__FILE__, '.'), 1);
 include_once("common.php");
 include_once("includes/functions_content.php");
 include_once("includes/functions_display.php");
 include_once("my_bbcode.php");
This is at the top of my embedded file

Code: Select all

 while ( $posts_text_array = mysql_fetch_array($posts_text_query) ) {

	 $posts_text_array['bbcode_options'] = (($posts_text_array['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0) +
		 (($posts_text_array['enable_smilies']) ? OPTION_FLAG_SMILIES : 0) +
		 (($posts_text_array['enable_magic_url']) ? OPTION_FLAG_LINKS : 0);

	 $message = generate_text_for_display($posts_text_array['post_text'], $posts_text_array['bbcode_uid'], $posts_text_array['bbcode_bitfield'], $posts_text_array['bbcode_options']);
And this is the top of the while loop that generates each news box. Later in the while loop, $message is simply echoed.

Please forgive my terrible organization and code optimization. I swear I go through and clean this kind of thing up when I get it working :'(
squingynaut
Registered User
Posts: 6
Joined: Thu Nov 25, 2004 3:03 am

Re: How to use phpBB3 bbcode parser?

Post by squingynaut »

Any ideas? :(
Rahabib
Registered User
Posts: 19
Joined: Thu Mar 31, 2005 9:22 pm

Re: How to use phpBB3 bbcode parser?

Post by Rahabib »

I have been trying all of this for awhile now and still cant get it to work.

Code: Select all

$test_id = 31;
								
$query   = "SELECT post_subject, post_text, bbcode_uid, bbcode_bitfield, enable_bbcode, enable_smilies, enable_magic_url FROM tx_posts WHERE post_id = '$test_id'";
$result  = mysql_query($query) or die('Error : ' . mysql_error()); 
$row     = mysql_fetch_array($result, MYSQL_ASSOC); 
    
$text = $row['post_text'];
$post_subject = $row['post_subject'];
$bitfield = $row['bbcode_bitfield'];
$uid = $row['bbcode_uid'];

$options = $row['bbcode_options'] = (($row['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0) +
    (($row['enable_smilies']) ? OPTION_FLAG_SMILIES : 0) + 
    (($row['enable_magic_url']) ? OPTION_FLAG_LINKS : 0);
	
define('IN_PHPBB', true);
$phpbb_root_path = '../board/';
$phpEx = 'php';
include ($phpbb_root_path . 'common.php');
include ($phpbb_root_path . 'includes/functions_display.php');
$user->session_begin();
$auth->acl($user->data);
$user->setup();

//$text = 'hello, [b]world[/b] http://www.abc.com :) ;) ';

/* to enable bbcode, urls and smilies parsing, be enable it when using 
generate_text_for_stoarge function */
generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true);

$text = generate_text_for_display($text, $uid, $bitfield, $options);

echo $text;
Ok, it works as long as I use the commented $text = 'hello, world http://www.abc.com :) ;) '; But when I bring in anything from the db it keeps bbcode_uid still in the bbtags and none of the bbtags work.
[b:b1a56]Test[/b:b1a56] test
[b:b1a56]testing[/b:b1a56] http://www.google.com
I assume its just not recognizing the bbcode_uid or something. Any ideas? I hope its just an ID10T error on my part.
User avatar
A_Jelly_Doughnut
Former Team Member
Posts: 34459
Joined: Sat Jan 18, 2003 1:26 am
Location: Where the Rivers Run
Contact:

Re: How to use phpBB3 bbcode parser?

Post by A_Jelly_Doughnut »

Move this code to the start of your snippet.

Code: Select all

define('IN_PHPBB', true);
$phpbb_root_path = '../board/';
$phpEx = 'php';
include ($phpbb_root_path . 'common.php');
include ($phpbb_root_path . 'includes/functions_display.php');
$user->session_begin();
$auth->acl($user->data);
$user->setup();
The $bbcode_options is not correctly calculated because this doesn't come before that calculation :)
A Donut's Blog
"Bach's Prelude (Cello Suite No. 1) is driving Indiana country roads in Autumn" - Ann Kish
Rahabib
Registered User
Posts: 19
Joined: Thu Mar 31, 2005 9:22 pm

Re: How to use phpBB3 bbcode parser?

Post by Rahabib »

ah figured it out.

apparently generate_text_for_storage uses $uid and $bitfield, which overwrote my existing variables.

I changed the variables to $uid2 and $bitfield2 and it worked. Once I did that and got it working I found I didnt even need generate_text_for_storage and it all worked fine from there.

thanks.

for reference:

Code: Select all

define('IN_PHPBB', true);
$phpbb_root_path = '../forum/';
$phpEx = 'php';
include ($phpbb_root_path . 'common.php');
include ($phpbb_root_path . 'includes/functions_display.php');
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// --> Open DB
include 'YOURDBPOPENFILE.php';

$test_id = 4;
								
$query   = "SELECT post_subject, post_text, bbcode_uid, bbcode_bitfield, enable_bbcode, enable_smilies, enable_magic_url FROM YOURDB_posts WHERE post_id = '$test_id'";
$result  = mysql_query($query) or die('Error : ' . mysql_error()); 
$row     = mysql_fetch_array($result, MYSQL_ASSOC); 
    
$text = $row['post_text'];
$post_subject = $row['post_subject'];
$bitfield = $row['bbcode_bitfield'];
$uid = $row['bbcode_uid'];

$options = $row['bbcode_options'] = (($row['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0) +
    (($row['enable_smilies']) ? OPTION_FLAG_SMILIES : 0) + 
    (($row['enable_magic_url']) ? OPTION_FLAG_LINKS : 0);
	
//$text = 'hello, [b]world[/b] http://www.abc.com :) ;) '; /* test code in case db pull isnt working */

/* to enable bbcode, urls and smilies parsing, be enable it when using 
generate_text_for_stoarge function */

$text = generate_text_for_display($text, $uid, $bitfield, $options);

echo $text;

// <-- Close DB
include 'YOURDBPCLOSEFILE.php';

Capcy
Registered User
Posts: 1
Joined: Mon May 19, 2008 8:18 am

Re: How to use phpBB3 bbcode parser?

Post by Capcy »

Code: Select all

<?php
define('IN_PHPBB', true);

// You set these values!
$phpbb_root_path = '';
$forum_id = '3';
$limit    = '10';

// Leave these alone!
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require_once($phpbb_root_path . 'common.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions_display.php');
$user->session_begin();
$auth->acl($user->data);
$user->setup(); 

// Grab info from the database.
$result = $db->sql_query("SELECT * FROM {$table_prefix}posts WHERE forum_id='{$forum_id}' LIMIT $limit");

// Show all records.
while($row = $db->sql_fetchrow($result)) {

  // You can edit these deatils.

  echo "<span>";
  echo "<a href='{$phpbb_root_path}viewtopic.php?f={$forum_id}&t={$row['topic_id']}'>{$row['post_subject']}</a>";
  echo "<br />";
  echo generate_text_for_display(smiley_text($row['post_text']), $row['bbcode_uid'], $row['bbcode_bitfield'], $row['enable_bbcode'] = $row['enable_smilies'] = $row['enable_magic_url'] = true);
  echo "<br />";
    $uresult = $db->sql_query("SELECT * FROM {$table_prefix}users WHERE user_id='{$row['poster_id']}'");
    $urow    = $db->sql_fetchrow($uresult);
  echo "<a href='{$phpbb_root_path}memberlist.php?mode=viewprofile&u={$row['poster_id']}'>{$urow['username']}</a>";
  echo "</span>";

  // Just don't go past here.
  
}
// Close database connection.
$db->sql_close();
?>
Updated!

My Site (Example): http://capcy.torchs.net/
dinsdale
Registered User
Posts: 9
Joined: Fri May 09, 2008 2:23 pm

Re: How to use phpBB3 bbcode parser?

Post by dinsdale »

Capcy wrote:

Code: Select all

<?php
  echo generate_text_for_display(smiley_text($row['post_text']), $row['bbcode_uid'], $row['bbcode_bitfield'], $row['enable_bbcode'] = $row['enable_smilies'] = $row['enable_magic_url'] = true);
[/quote]
Very well done!  This bit was the key bit.  I was having trouble getting scripts posted on this thread to parse urls marked up with bbcode.  The example used was just a raw url which to me was not the typical case.  I believe the inclusion of enable_magic_url might've made the difference for my case.  Your post was the first on this topic to include that bit of information.  I like also the way you consolidated all the steps into one line.  Thansky versky muchky!  :D
NearlyALaugh
Registered User
Posts: 14
Joined: Sun Mar 23, 2008 9:24 pm

Re: How to use phpBB3 bbcode parser?

Post by NearlyALaugh »

Can the above code be generalized to allow generate_text_for_display() to apply bbcodes and smilies to any phpbb-generated string with bitfield and uid data (etc.)?

(I'm working on a php script that selects and displays certain private messages, and I want to output them with smilies and proper html.)

These appear to be the essential parts of the above code:

Code: Select all

define('IN_PHPBB', true);

$phpbb_root_path = '../forum/';

$phpEx = substr(strrchr(__FILE__, '.'), 1);
require_once($phpbb_root_path . 'common.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions_display.php');

Code: Select all

echo generate_text_for_display(smiley_text($row['message_text']), $row['bbcode_uid'], $row['bbcode_bitfield'], $row['enable_bbcode'] = $row['enable_smilies'] = $row['enable_magic_url'] = true);
A "General Error" message is generated -- "The file ../forum/styles//template/bbcode.html is missing." -- when my script tries to run the function.

Any ideas?
User avatar
EXreaction
Former Team Member
Posts: 5666
Joined: Sun Aug 21, 2005 9:31 pm
Location: Wisconsin, U.S.
Name: Nathan

Re: How to use phpBB3 bbcode parser?

Post by EXreaction »

You have to begin the session.
NearlyALaugh
Registered User
Posts: 14
Joined: Sun Mar 23, 2008 9:24 pm

Re: How to use phpBB3 bbcode parser?

Post by NearlyALaugh »

Perfect! Thanks!
Phicha
Registered User
Posts: 54
Joined: Sat Feb 19, 2005 4:08 pm
Location: Medan

Re: How to use phpBB3 bbcode parser?

Post by Phicha »

i think got the same problem and not solved yet on http://www.phpbb.com/community/viewtopi ... &p=6782395

will someone help me ?

thanks
http://www.parkirotak.com
the new community.
http://www.sharecode.co.cc
the new way sharing your code.
kp3011
Registered User
Posts: 480
Joined: Fri Aug 27, 2004 4:58 am

Re: How to use phpBB3 bbcode parser?

Post by kp3011 »

Sorry for pushing up the topic, but here is the topic which fits my problem most.

I'm using a code similar to blueray2048's one at http://www.phpbb.com/community/viewtopi ... 5#p3425095 . However it is not parsing QUOTEs, though it is parsing other BBCodes correctly.

INPUT

Code: Select all

[quote="234"]123[/quote] [color=red]hello[/color], [b]world[/b] http://www.abc.com :) ;) 
OUTPUT

Code: Select all

[quote="234"]123[/quote] [color=red:3d9fqx9e]hello[/color:3d9fqx9e], [b:3d9fqx9e]world[/b:3d9fqx9e] http://www.abc.com :) ;)
(I didn't call generate_text_for_display, because I want to save the output into the database)

Any ideas or tricks I should have used but missed? Thanks!
PlasmaAu
Registered User
Posts: 11
Joined: Wed Mar 17, 2010 12:33 pm

Re: How to use phpBB3 bbcode parser?

Post by PlasmaAu »

Hi,

I've had to track down this problem too.

I managed to get , etc tags to parse fine, but hit a road block with the quote tag.

The reason the quote tag didn't parse is because it relies on a 'second stage' pass to parse correctly (otherwise, you may be left with no closing quote tag, for example).

In order to take existing, encoded phpbb post data (that you would find in the phpbb_posts table for example), and render it as HTML, you need to just do this:

// Ensure the user's session has started, and is setup:
$user->session_begin();
$auth->acl($user->data);
$user->setup();

If you fail to call setup() on the $user instance, then you will not have quotes parsed correctly (but etc would work fine).

Then you can call parse_text_for_display to render HTML:

$html = generate_text_for_display($text, $bbcode_uid, $bbcode_bitfield, OPTION_FLAG_BBCODE | OPTION_FLAG_SMILIES | OPTION_FLAG_LINKS);

Where $text is the contents of 'post_text' from phpbb_posts, $bbcode_uid and $bbcode_bitfield are also found within the phpbB_posts table.

$html will then contain your parsed HTML (according to your template).
Locked

Return to “[3.0.x] MOD Writers Discussion”