PDA

View Full Version : [PHP] BBCode System



Wise
02-03-10, 12:32 AM
Maverfax told me to make one, so I did.



<?php
/*************************************************
/* *
/* Author: Adam Višarsson *
/* Project: Learning PHP 8) *
/* *
/* bbcode.php - BBCode system. *
/* *
/************************************************/
function bbcode($text) {

$text = strip_tags($text, '<br />');

$search = array('/\(.*?)\[\/b\]/i',
'/\(.*?)\[\/i\]/i',
'/\(.*?)\[\/u\]/i',
'/\[url=(.*?)\](.*?)\[\/url\]/i',
'/\[code\](.*?)\[\/code\]/i',
'/\[img\](.*?)\[\/img\]/i',
'/\:D/i',
'/\:P/i',
'/\:\)/i',
'/\:\(/i',);

$h_toda_tml = array('<strong>$1</strong>',
'<i>$1</i>',
'<u>$1</u>',
'<a href="$1">$2</a>',
'<code>$1</code>',
'<img src="$1" alt="image" />',
'<img src="http://septimuswow.com/forums/public/style_emoticons/default/biggrin.gif" alt="grin" />',
'<img src="http://septimuswow.com/forums/public/style_emoticons/default/tongue.gif" alt="tongue" />',
'<img src="http://septimuswow.com/forums/public/style_emoticons/default/smile.gif" alt="smile" />',
'<img src="http://septimuswow.com/forums/public/style_emoticons/default/sad.gif" alt="sad" />',);

$text = preg_replace($search, $h_toda_tml, $text);

return $text;
}
?>

Go wild, make your own arrays \o/, don't forget to change the smiley urls if you want to have your own.

[U]How to install this?
First of all your page must include the page/script holding the bbcode system, if you save the above code as bbcode.php, you can include it like this:

<?php include('bbcode.php');?>

How to use it?
To make stuff display using the BBcode system in the right BBcodes, you need to echo it by doing this:

<?php echo bbcode('Hello guise, this text is Bold! And this is Underlined');?>
That would output:
Hello guise, this text is Bold! And this is Underlined


[I][B]- CREDITS TO Pingue 8)