Jump to content

BBcodes


phpwiz

Recommended Posts

ok, i was wondering how i could add BBcodes to my Post_news.php, Current_news.php & Post_comment.php, News_comments.php (i will post all the codes below) i have went on google, to many forums and i cannot find what i am looking for i have the BBcode things (got it from google), i will post that below too, can some one PLEASE help me with this, thanks ;)

 

Current_news.php

<?php


$connect = mysql_connect('fdb2.awardspace.com', 'tpnrpg_main', 'xxxxxx') or die("Couldnt connect");
mysql_select_db('xxxxxx') or die("Couldn't find db");

   $namecheck = mysql_query("SELECT username FROM Users");
   $count = mysql_num_rows($namecheck);

if ($_SESSION['username'])
{
if(isset($_GET['id'])){
//if the id is set in the url dislpay the selected id
$id = mysql_real_escape_string($_GET['id']);
$query='SELECT * FROM News WHERE id= ' . $id . ' ORDER BY id DESC LIMIT 1';
}else{
//if the id is not set in the url just display latest news
$query='SELECT * FROM News ORDER BY id DESC LIMIT 1';
}
//query the db
$getnews = mysql_query ($query) or die(mysql_error()) ;
while ($row = mysql_fetch_assoc ($getnews) )
{

//get data
$id = $row ['id'] ;
$title = $row ['title'] ;
$body = $row ['body'] ;
$date = $row ['date'] ;
$postee = $row ['postee'] ;
$avatar = $row ['avatar'] ;

echo "<table border='1' width='400px'>";
echo "<tr>
<td bgcolor='#E7E29A' colspan='2'><div align='center'><font color='#000000'>Subject: $title</font></div></td>
</tr>";
echo "<tr>
<td bgcolor='#FFFFFF'><div align='center'><text align='left'><font color='#000000'><img src='$avatar'></td>
<td bgcolor='#FFFFFF'><div align='center'><text align='left'><font color='#000000'><br><br><br><div class='text'>"; echo nl2br($body); echo"</div><br><br><br></font></div></td>
</td>
</tr>
<tr>
<td bgcolor='#E7E29A' colspan='2'><div align='center'><font color='#000000'>Posted On <i>$date</i></font></div></td>
</tr>
<tr>
<td bgcolor='#E7E29A' colspan='2'><div align='center'><font color='#000000'>Posted By: <b>$postee</b></font></div></td>
</tr>
</table>";
echo "<br>";

echo "
<table border='1' width='400px'>
<tr>
<td bgcolor='#E7E29A' colspan='2'><div align='center'><font color='#000000'>General Options</font></div></td>
</tr>
<tr>
<td bgcolor='#FFFFFF' colspan='2'><div align='center'><font color='#000000'><a href='News_comments.php'>[ View Comments ]</a></font></div></td>
</tr>
<tr>
<td bgcolor='#FFFFFF' colspan='2'><div align='center'><font color='#000000'><a href='Post_comment.php'>[ Create a comment ]</a></font></div></td>
</tr>
</table>
<br>
";

}

}
else
   die("Sorry you are not logged in <a href='login.php'> Click here</a> to login");

?>

 

Post_news.php

<?php

include "BBcodes.php";

include("admin.php");

$connect = mysql_connect('fdb2.awardspace.com', 'tpnrpg_main', 'xxxxxx') or die("Couldnt connect");
  mysql_select_db('xxxxxx') or die("Couldn't find db");

  $namecheck = mysql_query("SELECT username FROM Users");
  $count = mysql_num_rows($namecheck);

if ($_SESSION['username'])
{

if ($_POST ['post'])
{

//get data
$title = $_POST ['title'];
$body = $_POST ['body'];
       $date = $_POST ['date'];
       $postee = $_SESSION['username'];
       $avatar = $_POST ['avatar'];

//check for existance
if ($title&&$body&&$avatar)
{
$date = date ("Y-m-d");

//insert data
$insert = mysql_query ("INSERT INTO News VALUES ('','$title','$body','$date','$postee','$avatar')") or die (mysql_error ());

echo "Your news has been posted! Go <a href='Current_News.php'>HERE</a> to view it!";
}




else
{
echo "Please fill out title and body!<p>";
}
}
else
{



echo "<form action='Post_news.php' method='POST'>";
echo "Title:";
echo "<input type='text' name='title'><p>";
echo "Avatar link:";
echo "<input type='text' name='avatar'><p>";
echo "Body:<br>";
echo "<textarea rows='6' cols='35' name='body'></textarea><p>";
echo "<input type='submit' name='post' value='Post this news!'>";
echo "</form>";

}
}
else
die("Sorry you are not logged in <a href='login.php'> Click here</a> to login");

?>

 

News_comments.php

<?php

$connect = mysql_connect('fdb2.awardspace.com', 'tpnrpg_main', 'xxxxxx') or die("Couldnt connect");
  mysql_select_db('xxxxxx') or die("Couldn't find db");

  $namecheck = mysql_query("SELECT username FROM Users");
  $count = mysql_num_rows($namecheck);

if ($_SESSION['username'])
{

//query the db
$getnews = mysql_query ("SELECT * FROM News ORDER BY id DESC LIMIT 1") or die(mysql_error());

while ($row = mysql_fetch_assoc ($getnews) )
{

//get data
$id = $row ['id'];
$title = $row ['title'];
$body = $row ['body'];
$date = $row ['date'];
$postee = $row ['postee'];
$avatar = $row ['avatar'];
echo "<table border='1' width='450px'>";
echo "<tr>
<td bgcolor='#E7E29A' colspan='2'><div align='center'><font color='#000000'>Subject: $title</font></div></td>
</tr>";
echo "<tr>
<td bgcolor='#FFFFFF'><div align='center'><text align='left'><font color='#000000'><img src='$avatar'></td>
<td bgcolor='#FFFFFF'><div align='center'><text align='left'><font color='#000000'><div class='text'>"; echo nl2br($body); echo"</div></font></div></td>
</td>
</tr>
<tr>
<td bgcolor='#E7E29A' colspan='2'><div align='center'><font color='#000000'>Posted On <i>$date</i></font></div></td>
</tr>
<tr>
<td bgcolor='#E7E29A' colspan='2'><div align='center'><font color='#000000'>Posted By: <b>$postee</b></font></div></td>
</tr>
</table>";
echo "<br>";
echo "<table border='1' width='450px'>";
echo "
<tr>
<td bgcolor='#E7E29A'><div align='center'><font color='#000000'>Alternate Page Options</font></div></td>
</tr>
<tr>
<td bgcolor='#FFFFFF'><div align='center'><font color='#000000'><a href='Post_comment.php'>[ Post a comment ]</a></font></div></td>
</tr>
";
echo "</table>";
echo "<br>";
}
//query the db comments
$getnews1 = mysql_query ("SELECT * FROM Comments ORDER BY id DESC") or die(mysql_error());

while ($row1 = mysql_fetch_assoc($getnews1) )
{

//get data comments
$id1 = $row1 ['id'];
$body1 = $row1 ['body'];
$date1 = $row1 ['date'];
$postee1 = $row1 ['postee'];

echo "<table border='1' width='450px'>";
echo "<tr>
<td bgcolor='#10A608' colspan='2'><div align='center'><font color='#000000'>News comment</font></div></td>
</tr>";
echo "<tr>
<td bgcolor='#FFFFFF' width='100px'><div align='center'><text align='left'><font color='#000000'><img src='http://www.pokemonelite2000.com/sprites/frlgemtr/frlgemtr113.png'></td>
<td bgcolor='#FFFFFF'><div align='center'><text align='left'><font color='#000000'><div class='text'>"; echo nl2br($body1); echo"</div></font></div></td>
</td>
</tr>
<tr>
<td bgcolor='#E7E29A' colspan='2'><div align='center'><font color='#000000'>Posted On <i>$date1</i></font></div></td>
</tr>
<tr>
<td bgcolor='#E7E29A' colspan='2'><div align='center'><font color='#000000'>Posted By: <b>$postee1</b></font></div></td>
</tr>
<tr>
<td bgcolor='#10A608' colspan='2'><div align='center'><font color='#000000'>Thankyou for reading this News Comment!</font></div></td>
</tr>
</table>
<br>";


}



}

else
{
die("Sorry you are not logged in <a href='login.php'> Click here</a> to login");
}
?>

 

Post_comment.php

<?php

$connect = mysql_connect('fdb2.awardspace.com', 'tpnrpg_main', 'xxxxxx') or die("Couldnt connect");
   mysql_select_db('txxxxxx') or die("Couldn't find db");

   $namecheck = mysql_query("SELECT username FROM Users");
   $count = mysql_num_rows($namecheck);

if ($_SESSION['username'])
{

if ($_POST ['post'])
{

//get data
    $body = $_POST ['body'] ;
    $date = $_POST ['date'];
    $postee = $_SESSION['username'];

//check for existance
if ($body)
{

}

 $connect = mysql_connect('fdb2.awardspace.com', 'tpnrpg_main', 'xxxxxx') or die("Couldnt connect");
   mysql_select_db('xxxxxx') or die("Couldn't find db");

$date = date ("Y-m-d");

//insert data
$insert = mysql_query ("INSERT INTO Comments VALUES ('','$body','$date','$postee')") or die (mysql_error ()) ;

die ("Your news has been posted! Go <a href='News_comments.php'>HERE</a> to view it!") ;



}
else
	echo "Please fill out body!<p>";



echo "<form action='Post_comment.php' method='POST'>";
echo "<table>";
echo "<tr>
<td>
<center>Comment:<br>
</td>
</tr>
<tr>
<td>
<textarea rows='6' cols='35' name='body'></textarea><p>
</td>
</tr>
<tr>
<td>
<center><input type='submit' name='post' value='Post this comment'>
</td>
</tr>";
echo "</table>";
echo "</form>";

}
else
die("Sorry you are not logged in <a href='login.php'> Click here</a> to login");

?>

 

BBcodes.php

<?php
function bbcode($text){
   $text = str_replace('<', '<', $text);
   $text = str_replace('>', '>', $text);
   $text = str_replace( '', "", $text);
   
   $text = str_replace('[b]', '<b>', $text);
   $text = str_replace('[/b]', '</b>', $text);
   $text = str_replace('[i]', '<i>', $text);
   $text = str_replace('[/i]', '</i>', $text);
   $text = str_replace('[u]', '<u>', $text);
   $text = str_replace('[/u]', '</u>', $text);
   $text = str_replace('[url=', '<a href="', $text);
   $text = str_replace('[/url]', '</a>', $text);
   $text = str_replace('[img]', '<img src="', $text);
   $text = str_replace('[/img]', '">', $text);
   $text = str_replace('[c]', '<center>', $text);
   $text = str_replace('[/c]', '</center>', $text);
   $text = str_replace(']', '">', $text);
   
   $text = str_replace('[b]', '<b>', $text);
   $text = str_replace('[/b]', '</b>', $text);
   $text = str_replace('[i]', '<i>', $text);
   $text = str_replace('[/i]', '</i>', $text);
   $text = str_replace('[u]', '<u>', $text);
   $text = str_replace('[/u]', '</u>', $text);
   $text = str_replace('[url=', '<a href="', $text);
   $text = str_replace('[/url]', '</a>', $text);
   $text = str_replace('[img]', '<img src="', $text);
   $text = str_replace('[/img]', '">', $text);
   $text = str_replace('[C]', '<center>', $text);
   $text = str_replace('[/C]', '</center>', $text);
   $text = str_replace(']', '">', $text);
   
   $text = nl2br($text);
   $text = trim($text);
   
   return $text;
} 
?>

 

can you please tell me how to do this, thankyou!

 

 

Link to comment
https://forums.phpfreaks.com/topic/167736-bbcodes/
Share on other sites

<?php
function bbcode($text){
$arrayBBCode=array(
    ''=>         array('type'=>BBCODE_TYPE_ROOT,  'childs'=>'!i'),
    'i'=>        array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<i>',
                    'close_tag'=>'</i>', 'childs'=>'b'),
    'url'=>      array('type'=>BBCODE_TYPE_OPTARG,
                    'open_tag'=>'<a href="{PARAM}">', 'close_tag'=>'</a>',
                    'default_arg'=>'{CONTENT}',
                    'childs'=>'b,i'),
    'img'=>      array('type'=>BBCODE_TYPE_NOARG,
                    'open_tag'=>'<img src="', 'close_tag'=>'" />',
                    'childs'=>''),
    'b'=>        array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<b>',
                    'close_tag'=>'</b>'),
    'center'=>        array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<center>',
                    'close_tag'=>'</center>'),
    'u'=>        array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<u>',
                    'close_tag'=>'</u>'),
    'img'=>        array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<img src="',
                    'close_tag'=>'" border="0">'),

);
$BBHandler=bbcode_create($arrayBBCode);
$text = bbcode_parse($BBHandler,$text);
return $text;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/167736-bbcodes/#findComment-884568
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.