Forum Moderators: coopster

Message Too Old, No Replies

php bbcode

where to use it?

         

nfs2

12:38 am on Feb 17, 2006 (gmt 0)

10+ Year Member



I have a simple site that allows people to post information through a form, and id like to disable html and make bbcode possible

I found online a code that does just that, but i have no idea where to use it. The code in full is below

  function output_post ($post) {

//Make safe any html
$post_no_html = htmlspecialchars($post);

//Make sure there is no whitespace at the end of the message
//It's conceivable that the user will start their message with whitespace
$post_abridged = chop($post_no_html);

//Callback function for preg_replace_callback below
function convert_for_html ($matches) {
$regex[0] = "[";
$regex[1] = "]";
$replace[0] = "[";
$replace[1] = "]";
ksort($regex);
ksort($replace);
$treated = str_replace($regex, $replace, $matches[1]);
$output = '<table class="code"><tr><td>Code:</td></tr><tr><td class="code_box">' . $treated . '</td></tr></table>';

return $output;
}

//Convert code tags
$code_treated = preg_replace_callback(
"/\[code\](.+?)\[\/code\]/s",
"convert_for_html",
$post_abridged);

//Arrays for the bbCode replacements
$bbcode_regex = array(0 => '/\[b\](.+?)\[\/b\]/s',
1 => '/\[i\](.+?)\[\/i\]/s',
2 => '/\[u\](.+?)\[\/u\]/s',
3 => '/\[quote\](.+?)\[\/quote\]/s',
4 => '/\[quote\=(.+?)](.+?)\[\/quote\]/s',
5 => '/\[url\](.+?)\[\/url\]/s',
6 => '/\[url\=(.+?)\](.+?)\[\/url\]/s',
7 => '/\[img\](.+?)\[\/img\]/s',
8 => '/\[color\=(.+?)\](.+?)\[\/color\]/s',
9 => '/\[size\=(.+?)\](.+?)\[\/size\]/s');

$bbcode_replace = array(0 => '<b>$1</b>',
1 => '<i>$1</i>',
2 => '<u>$1</u>',
3 => '<table class="quote"><tr><td>Quote:</td></tr><tr><td class="quote_box">$1</td></tr></table>',
4 => '<table class="quote"><tr><td>$1 said:</td></tr><tr><td class="quote_box">$2</td></tr></table>',
5 => '<a href="$1">$1</a>',
6 => '<a href="$1">$2</a>',
7 => '<img src="$1" alt="User submitted image" title="User submitted image"/>',
8 => '<span style="color:$1">$2</span>',
9 => '<span style="font-size:$1">$2</span>');

ksort($bbcode_regex);
ksort($bbcode_replace);

//preg_replace to convert all remaining bbCode tags
$post_bbcode_treated = preg_replace($bbcode_regex, $bbcode_replace, $code_treated);

//Convert new lines to <br />
$post_with_br = nl2br($post_bbcode_treated);

echo $post_with_br;
};

Now, my form is simple like this;

<form action="posting.php" method="post" name="update">
<table>
<tr>
<td colspan="2">
<center><b>HTML</b> is allowed</center></td>
</tr>
<tr>
<td>

Subject:</td>
<td>
<input type="text" name="subject" id="subject" size="45" maxlength="60" style="width:450px" tabindex="2" class="post"/>
</td>
</tr>
<tr>
<td>Message:</td>
<td>
<textarea name="message" id="message" rows="15" cols="35" style="width:450px"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<center> <input type="submit" name="update" value="Update!" /></center>
</td>
</tr>
</table>
</form>

[/code]

So where do i use the code?

dreamcatcher

8:22 am on Feb 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

You pass the data from the textarea into the function. So something like:

if (isset($_POST['update']))
{
output_post ($_POST['message']);
exit;
}

dc