Jump to content


BBCode from forums to HTML on website


5 replies to this topic

#1 thedanharkins

    Young Padawan

  • Members
  • Pip
  • 24 posts
  • Gender:Male
  • Location:Kalamazoo, Michigan
  • Interests:Graphics, Webdesign, Gaming, Game Customization, Parkour, Movie making.

Posted 05 March 2006 - 12:10 PM

I have this script a friend constructed, and I fixed up. This code grabs infromation from mysql phpbb posts and puts it in a form where it can be displayed on my website. Its basically my news script. The only problem is it doesn't change BB code. I need help with this. I tried asking this on w3schools forum, and I got some solutions that don't work.

The script that I am using as an example is my forumrules page, that takes the information from the rules forum, and displays the only topic there which contains the text of the rules

Link to forums rules(WARNING HAS SOME SWEARING): http://djdtm.com/ind...page=forumrules

The code to forum rules:
<?php
include("**********.php");
//Mysql Connect Script
	$result = mysql_query("SELECT * from phpbb_topics WHERE forum_id=25 AND topic_vote != 1 order by topic_id desc LIMIT 0, 1")
	or die("Invalid query: " . mysql_error());
	while($myrow=mysql_fetch_array($result))
	{
	$topic_id = $myrow["topic_id"];
	$sql = sprintf("Select * from phpbb_posts_text where post_subject='%s' order by post_id desc LIMIT 0, 1", $myrow["topic_title"]);
	$result2 = mysql_query($sql) or die("Couldn't get news post");

	while($rows2=mysql_fetch_array($result2))
	$post_body = markup($rows2["post_text"]);
	}
?>
	<?php printf("%s", $post_body);?>
I narrowed down all the useless code like tables to save room.
The forum_id which is 25 is the rules forum, and it only shows the first thread LIMIT 0,1

And as you can see the bbcode shows as, if bold:
[b:c82d668aeb]TEST[/b:c82d668aeb]
and italics:
[i:c82d668aeb]TeST[/i:c82d668aeb]

And the breaks aren't being shown either.

So how would I accomplish changing all the bbcode from phpbb to html code when its being displayed on my site, and setting the breaks in their propper positions.

Any help would be greatly appreciated.

Edited by DjDTM, 05 March 2006 - 12:11 PM.


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 05 March 2006 - 02:53 PM

In the tutorial on my site about this, i have a function to modify the BBcode, but it simply removes it and puts a placeholder in for it (and also convert line breaks to html <br>'s).

I would have added a function to completely parse the BBcode, but when i tried, i found a few things wierd about the way phpBB inserts the BBcode into the database.
There are a few forms, example:
[code:1:unq_id]foo[/code:1:unq_id]
[code:unq_id]foo[/code:unq_id]

And maybe one other that i'm forgetting. But in sort, there is no simple way to parse their BBcode to html, so i find it easier to simply remove it.
To convert the simple ones, like [ b ], i would look at some of the BBcode tutorials around here, and add a little on to the regex they use.
i.e.
$find = array('#\[b:([a-z0-9])\](.*?)[/b:\\1]#si',
			  '#\[i:([a-z0-9])\](.*?)[/i:\\1]#si');
$replace = array('<b>\\2</b>',
				 '<i>\\2</b>');
Keep in mind, this is untested, i'm just assuming it will work. In case you're curious, the closing BB tag uses a back reference to get the id in the opening tag, more info can be found in php.nets PCRE syntax guide.

Edited by rc69, 05 March 2006 - 03:00 PM.


#3 thedanharkins

    Young Padawan

  • Members
  • Pip
  • 24 posts
  • Gender:Male
  • Location:Kalamazoo, Michigan
  • Interests:Graphics, Webdesign, Gaming, Game Customization, Parkour, Movie making.

Posted 05 March 2006 - 08:24 PM

Wow, you are definately better than the people at w3schools. That was the best answer I have got.

I'll get testing right away, your solutions look promising.

Will this also work with the
[URL=http://www.LINK.com]Link[/URL]
tags?

Or combinations of the tags?

Edited by DjDTM, 05 March 2006 - 08:27 PM.


#4 thedanharkins

    Young Padawan

  • Members
  • Pip
  • 24 posts
  • Gender:Male
  • Location:Kalamazoo, Michigan
  • Interests:Graphics, Webdesign, Gaming, Game Customization, Parkour, Movie making.

Posted 05 March 2006 - 09:38 PM

Sorry for double post.
Alright this is what I got in forum rules now:
<?php
include("phpscripts/********.php");
include("phpscripts/post2sitefunction.php");
//Mysql Connect Script
	$result = mysql_query("SELECT * from phpbb_topics WHERE forum_id=25 AND topic_vote != 1 order by topic_id desc LIMIT 0, 1")
	or die("Invalid query: " . mysql_error());
	while($myrow=mysql_fetch_array($result))
	{
	$topic_id = $myrow["topic_id"];
	$sql = sprintf("Select * from phpbb_posts_text where post_subject='%s' order by post_id desc LIMIT 0, 1", $myrow["topic_title"]);
	$result2 = mysql_query($sql) or die("Couldn't get news post");

	while($rows2=mysql_fetch_array($result2))
	$post_body = markup($rows2["post_text"]);
	$post_body = str_replace("\n", "<br>", $post_body);
	}
?>

This is what I got inside post2sitefunctions.php:
<?php function markup($post_body)
{
	$find = array("#\[b:([a-z0-9])\](.*?)[/b:\\1]#si","#\[i:([a-z0-9])\](.*?)[/i:\\1]#si");
	$replace = array("<b>\\2</b>","<i>\\2</i>");
	$post_body = preg_replace($find, $replace, $post_body);
	return $post_body;
}
?>
Link again:
http://djdtm.com/ind...page=forumrules

Breaks are set for now, but the messed up bbcode is still there.

#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 05 March 2006 - 11:16 PM

Lol, my bbcode thing didn't work because i forgot to escape somethings, i knew something had to go wrong ^_^
$find = array('#\[b:([a-z0-9])\](.*?)\[/b:\\1\]#si',
			  '#\[i:([a-z0-9])\](.*?)\[/i:\\1\]#si');
That should work a little better. As for the other bbcodes, you can look at the various tutorials on how to parse bbcode, then just adapt them to phpBB's style like i did. The easiest tutorial i could find for you is here: http://www.pixel2lif...showtopic=10659 (just look at how phpBB inserts the various tags into the database, and adapt the regex to work with it).

#6 thedanharkins

    Young Padawan

  • Members
  • Pip
  • 24 posts
  • Gender:Male
  • Location:Kalamazoo, Michigan
  • Interests:Graphics, Webdesign, Gaming, Game Customization, Parkour, Movie making.

Posted 06 March 2006 - 05:33 PM

Ok Improvement:

I changed your ([a-z0-9]) to ([a-z0-9\-]+) and I got some major improvement.

Alright now some more testing...

Ok heres what I got that works perfect right now:
forumrules.inc:
<?php
include("***************");
include("phpscripts/post2sitefunction.php");
//Mysql Connect Script
	$result = mysql_query("SELECT * from phpbb_topics WHERE forum_id=25 AND topic_vote != 1 order by topic_id desc LIMIT 0, 1")
	or die("Invalid query: " . mysql_error());
	while($myrow=mysql_fetch_array($result))
	{
	$topic_id = $myrow["topic_id"];
	$sql = sprintf("Select * from phpbb_posts_text where post_subject='%s' order by post_id desc LIMIT 0, 1", $myrow["topic_title"]);
	$result2 = mysql_query($sql) or die("Couldn't get news post");

	while($rows2=mysql_fetch_array($result2))
	{
	$post_body = str_replace("\n", "<br>", $rows2["post_text"]);
	$post_body = markup($post_body);
	}
	}
?>
And 2 the magic post2sitefunction.php:
<?php function markup($post_body)
{
$find = array(
	'@\[(?i)b:([a-z0-9\-]+)\](.*?)\[/(?i)b:([a-z0-9\-]+)\]@si',
	'@\[(?i)i:([a-z0-9\-]+)\](.*?)\[/(?i)i:([a-z0-9\-]+)\]@si',
	'@\[(?i)u:([a-z0-9\-]+)\](.*?)\[/(?i)u:([a-z0-9\-]+)\]@si',
	'@\[(?i)url=(.*?)\](.*?)\[/(?i)url\]@si',
	'@\[(?i)img:([a-z0-9\-]+)\](.*?)\[/(?i)img:([a-z0-9\-]+)\]@si'
);
$replace = array(
	'<b>\\2</b>',
	'<i>\\2</i>',
	'<u>\\2</u>',
	'<a href="\\1">\\2</a>',
	'<img src="\\2" alt="\\2" />'
);
	$post_body = preg_replace($find, $replace, $post_body);
	return $post_body;
}
?>

Does EXACTLY what I want it to do, this is so awesome thanks for giving me a kick start.

Edited by DjDTM, 06 March 2006 - 06:04 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users