Web Hosting Talk







View Full Version : How-To: PHP bbCode Function // Intermediate


JustinSmall
09-21-2008, 03:49 PM
It wouldn't let me post this is the tutorial section, I'm not sure why, so I'm posting it on here. :) Maybe they will move it, maybe they won't.

Anyways:
This is just a 'How To', it won't go into severe depth, it's for people who understand functions and intermediate level of PHP.

We'll begin with the function.

---------
bbcode.php

<?

function bbcode_format ($str) {
$str = htmlentities($str);

$start = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[a\=(.*?)\](.*?)\[\/a\]/is',
'/\[color\=(.*?)\](.*?)\[\/color\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[header\](.*?)\[\/header\]/is'
);

$finish = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
'<a href="$1">$2</a>',
'<span style="color: $1">$2</span>',
'<img src="$1" />',
'<span class="h3">$1</span>'
);


$str = preg_replace ($start, $finish, $str);

return $str;
}

?>

---------

now to use the code

---------
display.php

<?

require(bbcode.php);

echo bbcode_format('Text Here');

?>

---------

Any questions, ask... I'll try to answer :)