I found a BBcode tutorial here: http://www.phpit.net/article/create-bbcode-php/
How do I add this BBcode script to my script?
This is my script (show article):
<?php
include "../include/header.php";
include '../include/db.php';
$sql = mysql_query("SELECT article_id, user_id, title, ingress, text_1, image_1, text_2, image_2 FROM articles WHERE user_id = 1 ORDER BY title") or die (mysql_error());
while ( $row = mysql_fetch_array($sql) )
echo nl2br("<p><b><h1>" . $row["title"] . "</h1></b></p><p><b>" . $row["ingress"] . "</b></p><p>" . $row["text_1"] . "</p><p><img src=\"../images/article_images/" . $row["image_1"] . "\" alt=\"\"></p><p>" . $row["text_2"] . "</p><p><img src=\"../images/article_images/" . $row["image_2"] . "\" alt=\"\"></p>");
include "../include/footer.php";
?>
This is the BBcode:
function bbcode_format ($str) {
$str = htmlentities($str);
$simple_search = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is'
);
$simple_replace = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>'
);
// Do simple BBCode's
$str = preg_replace ($simple_search, $simple_replace, $str);
return $str;
}
function bbcode_quote ($str) {
$open = '<blockquote>';
$close = '</blockquote>';
// How often is the open tag?
preg_match_all ('/\[quote\]/i', $str, $matches);
$opentags = count($matches['0']);
// How often is the close tag?
preg_match_all ('/\[\/quote\]/i', $str, $matches);
$closetags = count($matches['0']);
// Check how many tags have been unclosed
// And add the unclosing tag at the end of the message
$unclosed = $opentags - $closetags;
for ($i = 0; $i < $unclosed; $i++) {
$str .= '</blockquote>';
}
// Do replacement
$str = str_replace ('[quote]', $open, $str);
$str = str_replace ('[/quote]', $close, $str);
return $str;
}