I'm trying to make a tutorial script... and I'm having some problems with bbcode+html tags
My addtutorial.php
<?
ob_start();
include("config.php");
?>
<?PHP
if($_POST['submit']){
$title = $_POST['title'];
$description = $_POST['description'];
$avatar = $_POST['avatar'];
$tutorial = nl2br(strip_tags($_POST['tutorial'],"<b><a><i><u><font color>"));
$author = $logged['username'];
$date = date("dS M Y");
$results = mysql_query("INSERT INTO tutorials (id, title, description, avatar, tutorial, author, date)
VALUES ('', '$title', '$description', '$avatar', '$tutorial', '$author', '$date' )") or die('Error making query'.mysql_error());
echo "The tutorial has successfully been added.";
}
?>
<table width='90%' cellpadding='0' cellspacing='0' style='border:1px solid #00000000;'>
<tr>
<td>
<center><form action="<? $_SERVER['PHP_SELF'] ?>" method="post">
<table width=\"400\" align=\"center\" cellpadding=\"2\" cellspacing=\"2\">
<tr>
<td width="150">Title:</td>
<td><input name="title" type="text"></td>
</tr>
<tr>
<td>Short description:</td>
<td><input name="description" type="text"></td>
</tr>
<tr>
<td>Link to avatar:</td>
<td><input name="avatar" type="text"></td>
</tr>
<tr>
<td valign="top">Tutorial:</td>
<td valign="top"><textarea name="tutorial" cols="30" rows="25"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input name="submit" type="submit" value="Submit"></td>
</tr>
</table>
</form></center>
</td></tr>
</table>
And my tutorial.php that show the tutorial (altso got a tutorials.php to show all of the tutorials... but I don't think there is a problem there 😉 )
<?
ob_start();
include("config.php");
?>
<?php
function bbcode_format ($str) {
$str = htmlentities($str);
$simple_search = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is',
'/\[url\](.*?)\[\/url\]/is',
'/\[align\=(left|center|right)\](.*?)\[\/align\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[mail\=(.*?)\](.*?)\[\/mail\]/is',
'/\[mail\](.*?)\[\/mail\]/is',
'/\[font\=(.*?)\](.*?)\[\/font\]/is',
'/\[size\=(.*?)\](.*?)\[\/size\]/is',
'/\[color\=(.*?)\](.*?)\[\/color\]/is',
);
$simple_replace = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
'<a href="$1">$2</a>',
'<a href="$1">$1</a>',
'<div style="text-align: $1;">$2</div>',
'<img src="$1" />',
'<a href="mailto:$1">$2</a>',
'<a href="mailto:$1">$1</a>',
'<span style="font-family: $1;">$2</span>',
'<span style="font-size: $1;">$2</span>',
'<span style="color: $1;">$2</span>',
);
$str = preg_replace ($simple_search, $simple_replace, $str);
$str = bbcode_quote ($str);
return $str;
}
function bbcode_quote ($str) {
$open = '<blockquote>';
$close = '</blockquote>';
preg_match_all ('/\[quote\]/i', $str, $matches);
$opentags = count($matches['0']);
preg_match_all ('/\[\/quote\]/i', $str, $matches);
$closetags = count($matches['0']);
$unclosed = $opentags - $closetags;
for ($i = 0; $i < $unclosed; $i++) {
$str .= '</blockquote>';
}
$str = str_replace ('[' . 'quote]', $open, $str);
$str = str_replace ('[/' . 'quote]', $close, $str);
return $str;
}
?>
<?PHP
$id = $_GET['id'];
$sql_events = mysql_query("SELECT * FROM tutorials WHERE id = '" . $id . "'") or die (mysql_error());
while ($row = mysql_fetch_array($sql_events)) {
$id = $row["id"];
$title = $row["title"];
$author = $row["author"];
$tutorial = bbcode_format($row["tutorial"]);
echo "
<table width=\"400\" align=\"center\" cellpadding=\"2\" cellspacing=\"2\">
<tr>
<td align=\"center\"><font size=\"2\" face=\"Tahoma\"><b>$title</b></font></td>
</tr>
<tr>
<td align=\"center\"><b>Created by:</b> <a href=\"index.php?page=Members&user=$author\">$author</a></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td align=\"left\" width=\"350\">$tutorial</td>
</tr>
</table>
";
}
?>
As you can see in addtutorial.php I'm using "$tutorial = nl2br(strip_tags($_POST['tutorial'],"<b><a><i><u><font color>");" that makes a <br> everytime you press enter
the problem is just that the bbcode "blocks" it
so if I write:
"This is enter a tutorial"
If I remove the bbcode it show up like this:
"This is
a tutorial"
And with the bbcode it shows up like this: "This is <br /> a tutorial"
I don't understand what EVERYTHING do in the script (still learning) so I need some help to fix this 🙂