Jump to content

BBCode among other things


Rosst

Recommended Posts

I made a bbcode function for my news system, I wanted to add a php tag and so I tried but it didn't work, here is my code:
[code]<?
function bbcode($content){
$content = nl2br(htmlspecialchars($content));

    $bbcode = array(
       "'\[b\](.*?)\[/b\]'",
       "'\[i\](.*?)\[/i\]'",
       "'\[u\](.*?)\[/u\]'",
       "'\[code\](.+?)\[/code\]'",
       "'\[url=(.*?)\](.*?)\[/url\]'",
       "'\[url\](.*?)\[/url\]'",
       "'\[img\](.*?)\[/img\]'",  
       "'\[php\](.*?)\[/php\]'"
        );
        
      $html = array(
        "<b>\\1</b>",
        "<i>\\1</i>",
        "<u>\\1</u>",
        "<strong>Code:</strong><div style=\"margin:0px 10px;padding:5px;border:1px dotted #FFFFFF;width:80%;\"></em>\\1</em></div>",
        "<a href=\"\\1\" target=\"_BLANK\">\\2</a>",
        "<a href=\"\\1\" target=\"_BLANK\">\\1</a>",
        "<img border=\"0\" src=\"\\1\">",
        "<strong>PHP:</strong><div style=\"margin:0px 10px;padding:5px;border:1px dotted #FFFFFF;width:80%;\"></em><?php highlight_sting(\"\\1\"); ?></em></div>
        );
      
    $content = preg_replace($bbcode, $html, $content);
    return nl2br($content);
}
?>[/code]
Oh also my news comments aren't showing up (I get the header and menu but no content) here is my code:
[code]<?php
ob_start(); // allows us to use cookies
include("config.php"); // includes the config file
if ($logged['username']){ // if the user is logged in they can view this

switch($_GET['view']){ // news_comments.php?view=
case 'news': // news_comments.php?view=news&id=$id

$id = $_GET['acl']; // grabs the requested id
$select = "select * from news where id=$id"; // selects the requested id from the database
$select2 = "select * from news_comments where nid=$id ORDER BY id DESC"; // grabs comments for the news article

$getnews = mysql_query($select) // querys the news db
or die(mysql_error()); // die error!
$getcomments = mysql_query($select2); // querys the comments db

if (mysql_num_rows($getnews) == "0") { // if we are returned a nulled result

echo 'Unable to find the article in our database'; // nothing found error!
exit(); // exit
}
$row = mysql_fetch_array($getnews) // array
or die(mysql_error()); // error!

$nid = $row['id']; // news id
$ntitle = $row['title']; // news title
$ncontent = bbcode($row['content']); // news content with bbcode wrapped around it
$nauthor = $row['author']; // news author
$postdate = $row['postdate']; // news date

    echo ("<tr>
                <td>$ntitle Posted by <a href=\"index.php?id=memb&user=$nauthor\">$nauthor</a> At $postdate<br></td>
            </tr>
               <tr>
                <td>$ncontent<br><br>Comments:<br></td>
            </tr>");
            
if (mysql_num_rows($getcomments) == "0") { // if we are returned a nulled result for the comments echo a link to add em
echo("There are no comments!<br>[<a href=\"index.php?id=newscom&view=addcomment&acl=$id\">Add a comment</a>]"); // add a link!
}
if($logged[username] && $logged[level] ==5) // if the user is a admin

while($rowc= mysql_fetch_array($getcomments)){ // fetches an array

$cauthor = $rowc['author']; // comment author
$ccomment = bbcode($rowc['content']); // comment content
$cdate = $rowc['postdate']; // comment date

    echo ("<tr>
                <td>Comment Posted by <a href=\"index.php?id=memb&user=$nauthor\">$cauthor</a> At $cdate [<a href=\"index.php?id=newad&view=editcomment&acl=$id\">Edit</a>] [<a href=\"index.php?id=newad&view=deletecomment&acl=$id\">Delete</a>]<br></td>
            </tr>
               <tr>
                <td>$ccomment<br><br></td>
            </tr>");    
}
while($rowc= mysql_fetch_array($getcomments)){ // if the user is a normal user echo this

$cauthor = $rowc['author']; // comment author
$ccomment = bbcode($rowc['content']); // comment content
$cdate = $rowc['postdate']; // comment date

    echo ("<tr>
                <td>Comment Posted by <a href=\"index.php?id=memb&user=$nauthor\">$cauthor</a> At $cdate<br></td>
            </tr>
               <tr>
                <td>$ccomment<br><br></td>
            </tr>");
}
echo("[<a href=\"index.php?id=newscom&view=addcomment&acl=$id\">Add a comment</a>]"); // add a comment
break;
case 'addcomment': // page for adding comments news_comments.php?view=addcomment&id=&id
ob_start(); // cookies
include("config.php"); // includes the config file

$id = $_GET['acl']; // grabs the news id
if(isset($_POST['add_comment'])) { // if the form has been submitted we query the db

$author = $logged['username']; // comments author
$postdate = date("g:i A, l F j"); // comments date
$content = $_POST[content]; // comment
$nid = $_GET['acl']; // news id

$sql = "INSERT INTO news_comments ( `author` , `postdate`, `content`, `nid`) VALUES ('$author', '$postdate', '$comment', '$nid')"; // query for adding the comment
$addblog = mysql_query($sql) // query?s the db
or die(mysql_error()); // error!

header("Location: index.php"); // redirects the user
}
else // the form has not been submitted so we display it
{
echo ("<form method=\"post\" name=\"addcomment\">
<tr>
    <td height=\"20\">Comment:</td>
</tr>
<tr>
    <td><br><textarea rows=\"5\" cols=\"35\" name=\"content\">Type your comment here!</textarea><br> <input type=\"submit\" name=\"add_comment\" value=\"Submit\"></td>
</tr>
</form>");
}
}
if (!isset($_GET['id'])) // if there is not requested id we redirect the user
header("Location:index.php?id=news"); // goto news.php
}else{
echo('Hey! you need to [<a href=http://vexxon.net/login.php>login</a>] first!'); // user is not logged in so they are asked to.');
}
?>[/code]
And my link system is not working either, here is the code for it:
[code]
<?php
ob_start(); // allows us to use cookies
include("config.php"); // includes the config file
if(!$logged[username]) {
header("Location:http://vexxon.net"); // if they arent logged in or dont have admin powers they are redirected
}
function cleanlink($url) {
$filter = array(
"http://" => "",
"www." => "",
);
foreach ($filter as $remove => $stay) {
$url = str_replace($remove, $stay, $url);
}
$filterurl = explode("/", $url);
$bombedurl = $filterurl[0];
$url = "http://";
$url .= "$bombedurl";
$url .= "/";
return $url;
}

if(isset($_POST['submit'])) {

$title=$_POST['title'];
$url= cleanlink($_POST['url']);
$description=$_POST['description'];
$sql = mysql_query("INSERT INTO `links` ( `title` , `url` , `description` ) VALUES ('$title', '$url', '$description');"); // adds the link to the db
header("Location:http://www.vexxon.net/index.php?id=links");

}
else {
?>
<form method="post" name="links">
<table width="52%" height="459" border="0">

<tr>
<td valign="top">Links Title: </td>
<td valign="top"><input name="title" type="text"></td>
</tr>
<tr>
<td valign="top">Links URL: </td>
<td valign="top"><input name="url" type="text" value="http://"></td>
</tr>
<tr>
<td height="85" valign="top">Description: <br> </td>
<td valign="top"><textarea cols="50" rows="20" name="description"></textarea>
</td>
</tr>
<tr>
<td height="25" valign="top"> </td>
<td valign="top"><input type="submit" name="submit" value="Submit Link"></td>
</tr>
</table>
</form>
<?
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/12339-bbcode-among-other-things/
Share on other sites

Try this:
[code]function bbcode($content)
{
    $bbcode = array(
       "'\[b\](.*?)\[/b\]'",
       "'\[i\](.*?)\[/i\]'",
       "'\[u\](.*?)\[/u\]'",
       "'\[code\](.+?)\[/code\]'",
       "'\[url=(.*?)\](.*?)\[/url\]'",
       "'\[url\](.*?)\[/url\]'",
       "'\[img\](.*?)\[/img\]'",
    );

    $html = array(
        "<b>\\1</b>",
        "<i>\\1</i>",
        "<u>\\1</u>",
        "<strong>Code:</strong><div style=\"margin:0px 10px;padding:5px;border:1px dotted #FFFFFF;width:80%;\"></em>\\1</em></div>",
        "<a href=\"\\1\" target=\"_BLANK\">\\2</a>",
        "<a href=\"\\1\" target=\"_BLANK\">\\1</a>",
        "<img border=\"0\" src=\"\\1\">",
    );

    $content = preg_replace($bbcode, $html, $content);

    // call a dedicated function to highlight our php code.
    $content = preg_replace_callback("#\[php\](.*?)\[\/php\]#is", 'highlight_php', $content);

    return nl2br($content);
}

function highlight_php($matches)
{
    $php = "<strong>PHP:</strong><div style=\"margin:0px 10px;padding:5px;border:1px dotted #FFFFFF;width:80%;\"></em>";
    $php .= highlight_string($matches[0], true);
    $php .= '</em></div>';

    //strip out line breaks:
    $php = str_replace("<br />", '', $php);

    // strip out PHP BBCode tags:
    $php = preg_replace("#(\[php\]|\[/php\])#i", "", $php);

    return $php;
}[/code]


[!--quoteo(post=385559:date=Jun 19 2006, 05:10 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 19 2006, 05:10 AM) [snapback]385559[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Try this:
[code]function bbcode($content)
{
    $bbcode = array(
       "'\[b\](.*?)\[/b\]'",
       "'\[i\](.*?)\[/i\]'",
       "'\[u\](.*?)\[/u\]'",
       "'\[code\](.+?)\[/code\]'",
       "'\[url=(.*?)\](.*?)\[/url\]'",
       "'\[url\](.*?)\[/url\]'",
       "'\[img\](.*?)\[/img\]'",
    );

    $html = array(
        "<b>\\1</b>",
        "<i>\\1</i>",
        "<u>\\1</u>",
        "<strong>Code:</strong><div style=\"margin:0px 10px;padding:5px;border:1px dotted #FFFFFF;width:80%;\"></em>\\1</em></div>",
        "<a href=\"\\1\" target=\"_BLANK\">\\2</a>",
        "<a href=\"\\1\" target=\"_BLANK\">\\1</a>",
        "<img border=\"0\" src=\"\\1\">",
    );

    $content = preg_replace($bbcode, $html, $content);

    // call a dedicated function to highlight our php code.
    $content = preg_replace_callback("#\[php\](.*?)\[\/php\]#is", 'highlight_php', $content);

    return nl2br($content);
}

function highlight_php($matches)
{
    $php = "<strong>PHP:</strong><div style=\"margin:0px 10px;padding:5px;border:1px dotted #FFFFFF;width:80%;\"></em>";
    $php .= highlight_string($matches[0], true);
    $php .= '</em></div>';

    //strip out line breaks:
    $php = str_replace("<br />", '', $php);

    // strip out PHP BBCode tags:
    $php = preg_replace("#(\[php\]|\[/php\])#i", "", $php);

    return $php;
}[/code]
[/quote]
You are the greatest, it worked, to see it go to www.vexxon.net/index.php?id=news
Some one please help me with this:
<?php
include("config.php");
include("include/bbcode.php");

$sql = mysql_query("SELECT*FROM `tuts_cats` ORDER BY id DESC");
if (!$_GET['cat']) {
if (mysql_num_rows($sql) == 0) {
echo "No tutorial categories";
}
else {
echo "<table border=0 width=100%>";
while ($r=mysql_fetch_array($sql)) {
echo ("
<tr>
<td><a href=\"index.php?id=tuts&cat=$r[category]\">$r[category]</td>
</tr>");
}
echo "</table>";
}
}
elseif ($_GET['cat']) {
$cat = $_GET['cat'];
$sql2 = mysql_query("SELECT*FROM `tuts` where cat = '$cat' ORDER BY id DESC");
if (mysql_num_rows($sql) == 0) {
echo "No tutorials in this category";
}
else {
echo "<table border=0 width=100%>";
while ($r2=mysql_fetch_array($sql2)) {
echo ("
<tr>
<td><a href=\"index.php?id=tuts&tut=$r2[id]\">$r2[tutname]</td>
</tr>
<tr>
<td>$r2[description]</td>
</tr>
");
}
echo "</table>";
}
}
else {
$tut = $_GET['tut'];
$sql3 = mysql_query("SELECT*FROM `tuts` where id = '$tut'");
$r3 = mysql_fetch_array($sql3);
$content = bbcode($r3[content]);
echo "<table border=0>
<tr>
<td>$r3[title] written by <a href=\"index.php?id=memb&user=$r3[author]\">$r3[author]</a></td>
</tr>
<tr>
<td>$r3[description], $r3[tutdate]</td>
</tr>
<tr>
<td>$content</td>
</tr>
</table>");
}
?>

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.