Jump to content

BBCode to HTML


Recommended Posts

Ok, so.I have a script that displays posts in a certain forum on the page that its in.Its my news script, that if I post posts in the news forum, it shows on my main website.It really sucks, and I was wondering if anyone can help me revise it and make it so it replaces all bbcode to html, so I can post urls and images on the forum and have it load on the website.Because what currently happens is if i have a bbcode tag, it shows the tag up on the site.Heres my code:

<table align="center" border="0" cellspacing="0" cellpadding="0" width="95%"><tr><td height="216"><?php	$conn = mysql_connect("database.net","databasename", "password")    or die("Could not connect: " . mysql_error());	//echo("Connection Successful<br/>");	mysql_select_db("databasename", $conn);		function AddEmoticons($post_body)	{  $r_smiles = mysql_query("Select * from phpbb_smilies") or die("couldn't retrieve smiles");  // good lets continue with getting smiles  while($smiles = mysql_fetch_array($r_smiles))  {  	$smile_code = $smiles["code"]; // what is it?  ? ? etc...  	//printf($smile_code);  	$smile_url = sprintf("http://djdtm.com/forums/images/smiles/%s", $smiles["smile_url"]);  	//printf($smile_url);  	$replace_html = sprintf("<img src='%s' alt='%s' />", $smile_url);  	$post_body = str_replace($smile_code, $replace_html, $post_body);    }  return $post_body;	}	$result = mysql_query("SELECT * from phpbb_topics WHERE forum_id=1 AND topic_vote != 1 order by topic_id desc LIMIT 0, 5")	or die("Invalid query: " . mysql_error());	//$myrow = mysql_fetch_array($result);	while($myrow=mysql_fetch_array($result))	{	$topic_id = $myrow["topic_id"];  // used for the comment system	$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))	{	// good lets get the post_id so we can grab the users avatar and "stuff"	$grabsql = sprintf("Select * from phpbb_posts where post_id=%d order by post_id desc", $rows2["post_id"]);	$result3 = mysql_query($grabsql) or     die("Something f****d up with the grabbing post info");	// good lets continue...	$postrow = mysql_fetch_array($result3);	$userid = $postrow["poster_id"];   //  who posted this mother f****r!	$date = date("F j, Y, g:i a", $postrow['post_time']);	$post_body = str_replace("\n", "<br/>", $rows2["post_text"]);	//$post_body = str_replace("", "<img style='filter: Alpha(Opacity=65, FinishOpacity=65, Style=0, StartX=0, StartY=0, FinishX=15, FinishY=0);' src='http://djdtm.com/forums/images/smiles/icon_biggrin.gif' alt='http://djdtm.com/forums/images/smiles/icon_biggrin.gif' />", $post_body);	AddEmoticons($post_body);	//=============================================================================	$usersql = sprintf("Select * from phpbb_users where user_id=%d", $userid);	$result4 = mysql_query($usersql) or die("Couldn't get user info");	$userinfo = mysql_fetch_array($result4);	// good lets grab the user's info.	$poster_name = $userinfo["username"];	$poster_avatar = $userinfo["user_avatar"];	$poster_email = $userinfo["user_email"];	$user_avatar_type = $userinfo["user_avatar_type"];	switch($user_avatar_type)	{  case 0:  // do nothing, it's a mother f****r  break;  case 1:  $avatarcrap = sprintf("http://djdtm.com/forums/images/avatars/%s", $poster_avatar);  break;  case 2:  $avatarcrap = sprintf("%s", $poster_avatar);  break;  case 3:  $avatarcrap = sprintf("http://djdtm.com/forums/images/avatars/gallery/%s", $poster_avatar);  break;	}?><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="center"><b><font color="#990000"><? printf("%s", $rows2["post_subject"]);?></font></b> - <? printf("%s", $date); ?></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td height="12"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td width="12"></td><td width="96" align="center" valign="top"><a href="http://djdtm.com/forums/profile.php?mode=viewprofile&u=<? echo $userid; ?>"><? printf("%s", $poster_name); ?></a><br/><img src="<? printf($avatarcrap); ?>" alt="<? printf($avatarcrap); ?>" /><br/><a href="<? printf("http://www.djdtm.com/forums/viewtopic.php?t=%d", $topic_id); ?>" target="_blank">View & Reply</a><a href="http://djdtm.com/forums/profile.php?mode=viewprofile&<? echo $userid; ?>"></a></td><td width="12" align="center"></td><td align="center" valign="top"><table border="0" cellspacing="0" cellpadding="0" width="100%">  <tr>    <td width="12" height="12" align="left" valign="top"></td>    <td valign="top" align="left"><font color="#990000" style="font-size: 11px;"><? printf("%s", $post_body);?></font></td>    <td width="12"></td>  </tr></table></td><td width="12"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>  <td height="12"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%">  <tr>    <td height="12" align="center" class="style4">----------------------------------------------------------------------------------------</td>  </tr></table>  <? }} ?>  </td></tr></table>

Sorry for the swearing, I didn't write this code. I don't know alot of what it does.It looks like the smileys are suppose to work but they don'tThe list of things I need help with:1.) Smileys to actually work2.) bbcode from post to be replaced with the propper html code on the website3.) revision a cleaner version..So any help would be greatly appreciated.EDIT:I was thinking about expanding the

$post_body = str_replace("\n", "<br/>", $rows2["post_text"]);

but how I would make it search multiple strings, and replace them with multiple strings.Such as, str_replace("","<b>",$rows2["post_text"]);, but how would I add multiple searches to that?Jeeze, I have no idea, some one help please.

Link to post
Share on other sites

So far I narrowed down all the useless code to this, which still seems to get me what I want, and thats JUST the post.

<table align="center" border="0" cellspacing="0" cellpadding="0" width="95%"><tr><td height="216" align="center"><?php	$conn = mysql_connect("CENSORED","CENSORED", "CENSORED)    or die("Could not connect: " . mysql_error());	//echo("Connection Successful<br/>");	mysql_select_db("CENSORED", $conn);		$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());	//$myrow = mysql_fetch_array($result);	while($myrow=mysql_fetch_array($result))	{	$topic_id = $myrow["topic_id"];  // used for the comment system	$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"]);	//==========================================?><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td width="12"></td><td align="center" valign="top"><table border="0" cellspacing="0" cellpadding="0" width="100%">  <tr>    <td width="12" height="12" align="left" valign="top"></td>    <td valign="top" align="left"><font color="#990000" style="font-size: 11px;"><? printf("%s", $post_body);?></font></td>    <td width="12"></td>  </tr></table></td><td width="12"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>  <td height="12"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%">  <tr>    <td height="12" align="center" class="style4">----------------------------------------------------------------------------------------</td>  </tr></table>  <? } ?>  <br />  <a href="http://www.djdtm.com/forums">Enter</a></td></tr></table>

If I can figure out where I can put the stuff you found for me in, I would have it all set.Cause I just want something like this on the forum

[URL=http://www.google.com][B]CLICKME[/B][/URL]

To end up on my website in the propper html code:

 <a href="http://www.google.com" target=_blank><b>CLICKME</b></a>

Obviously on the page it looks like thisCLICKMEBecause so far, all I get is code like this when its displayed through my site using this script:[b:95453fab54]ACTIVE[/b:95453fab54]So, I have no idea whatsup...

Edited by Jonas
Link to post
Share on other sites

The way I did it, is have it included in a function (the file is included) which cuts down a lot of code on your thread view page, thats the way I suggest doing it, so if you ever need to have it multiple place, it's very easy to edit.So, what'd you'd do is make a file, say, functions.php, and add in my code (read the thread, my original code doesn't work well).Then to call is later, you'd do

$post_body = markup($rows2["post_text"]);

Hope that helps. :)

Link to post
Share on other sites

Umm it kind of does, but it still doesn't work.All you told me to do was to put the code in a different document, but how does that affect way it shows?This is what I've got so far:in post2sitefunction.php I have this code:

<?php function markup($post_body){	$find = array("<", ">");	$replace = array("<", ">");	$post_body=str_replace($find, $replace, $post_body);		$find=array("/\[img\](.+?)\[\/img]/","/\[link name=(.+?)\](.+?)\[\/link\]/","/\[link\](.+?)\[\/link\]/","/\[b\]([^$]*)\[\/b\]/","/\[u\]([^$]*)\[\/u\]/","/\[i\]([^$]*)\[\/i\]/","/\[quote\]\[originator=(.+?)\]([^$]*)\[\/quote\]/","/\[quote\]([^$]*)\[\/quote\]/","/\[sup\]([^$]*)\[\/sup\]/","/\[sub\]([^$]*)\[\/sub\]/","/\[div align=(.+?)\]([^$]*)\[\/div\]/","/\[size=(.+?)\]([^$]*)\[\/size\]/","/\[anchor\](.+?)\[\/anchor\]/","/\[color=(.+?)\]([^$]*)\[\/color\]/","/([^'>])http:\/\/([^'<\s]+)/","/^http:\/\/([^'<\s]+)/");	$replace=array("<img src=$1>","<a href='$2'>$1</a>","<a href='$1'>$1</a>","<b>$1</b>","<u>$1</u>","<i>$1</i>","<br /><ul>quote <b>$1</b><br /><hr />$2<hr /></ul><br />","<br /><ul>quote<br /><hr />$1<hr /></ul><br />","<sup>$1</sup>","<sub>$1</sub>","<div align=$1>$2</div>","<font size=\"$1\">$2</font>","<a name=\"$1\"\></a>","<span style=\"color:$1\">$2</span>","$1<a href='http://$2'>http://$2</a>","<a href='http://$1'>http://$1</a>");$post_body=preg_replace($find, $replace, $post_body);return $post_body;}?>

and in forumrules I have this

<table align="center" border="0" cellspacing="0" cellpadding="0" width="95%"><tr><td height="216" align="center"><?php	$conn = mysql_connect("xxxxxxx","xxxxxxx", "xxxxxxx")    or die("Could not connect: " . mysql_error());	//echo("Connection Successful<br/>");	mysql_select_db("xxxxxxx", $conn);		$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());	//$myrow = mysql_fetch_array($result);	while($myrow=mysql_fetch_array($result))	{	$topic_id = $myrow["topic_id"];  // used for the comment system	$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));	include ("post2sitefunction.php");	$post_body = markup($rows2["post_text"]);	}?><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td width="12"></td><td align="center" valign="top"><table border="0" cellspacing="0" cellpadding="0" width="100%">  <tr>    <td width="12" height="12" align="left" valign="top"></td>    <td valign="top" align="left"><font color="#990000" style="font-size: 11px;">	<?php printf("%s", $post_body);?>	</font></td>    <td width="12"></td>  </tr></table></td><td width="12"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>  <td height="12"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%">  <tr>    <td height="12" align="center" class="style4">---------------------------------------------------------------------------------------</td>  </tr></table>  <br />  <a href="http://www.djdtm.com/forums">Enter</a></td></tr></table>

and you can see the results here:http://djdtm.com/index.php?page=forumruleswhich is:""What am I doing wrong?Ok, so I ripped some of your code, so sue me. But seriously I need some help.

Link to post
Share on other sites

I did, you just didn't look hard enough.Ok so I removed the space from the include.But it still doesn't change a thing.Current Code of forumrules:

<table align="center" border="0" cellspacing="0" cellpadding="0" width="95%"><tr><td height="216" align="center"><?php	$conn = mysql_connect("db213.perfora.net","dbo139021429", "3.YcQYTJ")    or die("Could not connect: " . mysql_error());	//echo("Connection Successful<br/>");	mysql_select_db("db139021429", $conn);		$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());	//$myrow = mysql_fetch_array($result);	while($myrow=mysql_fetch_array($result))	{	$topic_id = $myrow["topic_id"];  // used for the comment system	$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))	include("post2sitefunction.php");	$post_body = markup($rows2["post_text"]);	}?><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td width="12"></td><td align="center" valign="top"><table border="0" cellspacing="0" cellpadding="0" width="100%">  <tr>    <td width="12" height="12" align="left" valign="top"></td>    <td valign="top" align="left"><font color="#990000" style="font-size: 11px;">	<?php printf("%s", $post_body);?>	</font></td>    <td width="12"></td>  </tr></table></td><td width="12"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>  <td height="12"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%">  <tr>    <td height="12" align="center" class="style4">---------------------------------------------------------------------------------------</td>  </tr></table>  <br />  <a href="http://www.djdtm.com/forums">Enter</a></td></tr></table>

and then code for post2sitefunction.php

<?php function markup($post_body){	$find = array("<", ">");	$replace = array("<", ">");	$post_body=str_replace($find, $replace, $post_body);		$find=array("/\[img\](.+?)\[\/img]/","/\[link name=(.+?)\](.+?)\[\/link\]/","/\[link\](.+?)\[\/link\]/","/\[b\]([^$]*)\[\/b\]/","/\[u\]([^$]*)\[\/u\]/","/\[i\]([^$]*)\[\/i\]/","/\[quote\]\[originator=(.+?)\]([^$]*)\[\/quote\]/","/\[quote\]([^$]*)\[\/quote\]/","/\[sup\]([^$]*)\[\/sup\]/","/\[sub\]([^$]*)\[\/sub\]/","/\[div align=(.+?)\]([^$]*)\[\/div\]/","/\[size=(.+?)\]([^$]*)\[\/size\]/","/\[anchor\](.+?)\[\/anchor\]/","/\[color=(.+?)\]([^$]*)\[\/color\]/","/([^'>])http:\/\/([^'<\s]+)/","/^http:\/\/([^'<\s]+)/");	$replace=array("<img src=$1>","<a href='$2'>$1</a>","<a href='$1'>$1</a>","<b>$1</b>","<u>$1</u>","<i>$1</i>","<br /><ul>quote <b>$1</b><br /><hr />$2<hr /></ul><br />","<br /><ul>quote<br /><hr />$1<hr /></ul><br />","<sup>$1</sup>","<sub>$1</sub>","<div align=$1>$2</div>","<font size=\"$1\">$2</font>","<a name=\"$1\"\></a>","<span style=\"color:$1\">$2</span>","$1<a href='http://$2'>http://$2</a>","<a href='http://$1'>http://$1</a>");$post_body=preg_replace($find, $replace, $post_body);return $post_body;}?>

forumrules is loading post2sitefunctions correctly cause if I remove the include it gives me this error:Fatal error: Call to undefined function: markup() in /homepages/22/d120960397/htdocs/djdtm/forumrules.inc on line 21So there is obviously something wrong with post2sitefunctions. What is it though.You know giving me 1 line directions isn't going to help at all. So, could you give me more to work with.

Link to post
Share on other sites

I put the include up at the top before my connection code, still got nothing.I put in your code and got nothing.I put quotes around "post_text", and still got nothing.Doesn't anyone else know PHP? Jesus.Again:I tested again, If I removed the line:

$post_body = markup($post_body);

The post shows up propperly without the code replacement.So the problem lies with in that line of code, or the markup function...EDIT:Ok progress, I moved the

	return $post_body;

inside the brackets.And the post loads, without breaks, or correct replacement of the BBCODE.Then I changed the:

$post_body=$rows2["post_text"];$post_body = markup($post_body);

Back to:

$post_body = markup($rows2["post_text"]);

Same results as before. Shows the post, no breaks, no code replacement.You can see the test here:http://www.djdtm.com/index.php?page=forumrulesSee the orignal here:http://www.djdtm.com/forums/viewtopic.php?t=28You can see how the bbcode shows up all funky, and there are no breaks.Current forumrules:

<table align="center" border="0" cellspacing="0" cellpadding="0" width="95%"><tr><td height="216" align="center"><?phpinclude("connect2mysql.php");include("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"]);	}?><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td width="12"></td><td align="center" valign="top"><table border="0" cellspacing="0" cellpadding="0" width="100%">  <tr>    <td width="12" height="12" align="left" valign="top"></td>    <td valign="top" align="left"><font color="#990000" style="font-size: 11px;">	<?php printf("%s", $post_body);?>	</font></td>    <td width="12"></td>  </tr></table></td><td width="12"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>  <td height="12"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%">  <tr>    <td height="12" align="center" class="style4">---------------------------------------------------------------------------------------</td>  </tr></table>  <br />  <a href="http://www.djdtm.com/forums">Enter</a></td></tr></table>

post2sitefunction.php code:

<? function markup($post_body){	$find = array("<", ">");	$replace = array("<", ">");	$post_body=str_replace($find, $replace, $post_body);	$find=array("/\[img\](.+?)\[\/img]/","/\[url=(.+?)\](.+?)\[\/url\]/","/\[url\](.+?)\[\/url\]/","/\[b\]([^$]*)\[\/b\]/","/\[u\]([^$]*)\[\/u\]/","/\[i\]([^$]*)\[\/i\]/","/\[quote\]\[originator=(.+?)\]([^$]*)\[\/quote\]/","/\[quote\]([^$]*)\[\/quote\]/","/\[sup\]([^$]*)\[\/sup\]/","/\[sub\]([^$]*)\[\/sub\]/","/\[div align=(.+?)\]([^$]*)\[\/div\]/","/\[size=(.+?)\]([^$]*)\[\/size\]/","/\[anchor\](.+?)\[\/anchor\]/","/\[color=(.+?)\]([^$]*)\[\/color\]/","/([^'>])http:\/\/([^'<\s]+)/","/^http:\/\/([^'<\s]+)/");	$replace=array("<img src=$1 alt=$1/>","<a href src='$2'>$1</a>","<a href src='$1'>$1</a>","<b>$1</b>","<u>$1</u>","<i>$1</i>","<br /><ul>quote <b>$1</b><br /><hr />$2<hr /></ul><br />","<br /><ul>quote<br /><hr />$1<hr /></ul><br />","<sup>$1</sup>","<sub>$1</sub>","<div align=$1>$2</div>","<font size=\"$1\">$2</font>","<a name=\"$1\"\></a>","<span style=\"color:$1\">$2</span>","$1<a href src='$2'>http://$2</a>","<a href src ='http://$1'>http://$1</a>");	$post_body=preg_replace($find, $replace, $post_body);	return $post_body;}?>

If it helps i'm using PHPBB2.0.19 for all this.How do I fix this?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...