For those who don't have the BBCode extention installed it's not too difficult to do this from scratch.
<?
function bbparse(&$content) {
while (preg_match_all('`\[(.+?)=?(.*?)\](.+?)\[/\1]`', $content, $matches)) foreach ($matches[0] as $index => $match) {
list($tag, $param, $innertext) = array($matches[1][$index], $matches[2][$index], $matches[3][$index]);
switch ($tag) {
case "b": $replacement = "<strong>$innertext</strong>"; break;
case "i": $replacement = "<em>$innertext</em>"; break;
case "size": $replacement = "<span style=\"font-size: $param;\">$innertext</a>"; break;
case "color": $replacement = "<span style=\"color: $param;\">$innertext</a>"; break;
case "center": $replacement = "<div class=\"centered\">$innertext</div>"; break;
case "quote": $replacement = "<blockquote>$innertext</blockquote>" . $param? "<cite>$param</cite>" : ""; break;
case "url": $replacement = "<a href=\"" . ($param? $param : $innertext) . "\">$innertext</a>"; break;
case "img":
list($width, $height) = preg_split('`[Xx]`', $param);
$replacement = "<img src=\"$innertext\" " . (is_numeric($width)? "width=\"$width\" " : '') . (is_numeric($height)? "height=\"$height\" " : '') . '/>';
break;
case "video":
$videourl = parse_url($innertext);
parse_str($videourl['query'], $videoquery);
if (strpos($innertext, "youtube.com")) $replacement = '<embed src="http://www.youtube.com/v/' . $videoquery['v'] . '" type="application/x-shockwave-flash" width="425" height="344"></embed>';
if (strpos($innertext, "video.google.com")) $replacement = '<embed src="http://video.google.com/googleplayer.swf?docid=' . $videoquery['docid'] . '" type="application/x-shockwave-flash" width="400" height="326"></embed>';
break;
case "script": $replacement = ""; break;
default: $replacement = "<$tag>$innertext</$tag>"; break;
}
$content = str_replace($match, $replacement, $content);
}
}
?>
Basically this function will interperate any [tag=param]innertext[\tag] as valid and convert it to HTML, excluding [script].
If you want users to be limited to only the tags you specifiy, delete the default line in the switch statement. Alternatively you can add more cases with empty replacements to just exlude those tags.
Bulletin Board Code
- Introduction
- Installing/Configuring
- Predefined Constants
- BBCode Functions
- bbcode_add_element — Adds a bbcode element
- bbcode_add_smiley — Adds a smiley to the parser
- bbcode_create — Create a BBCode Resource
- bbcode_destroy — Close BBCode_container resource
- bbcode_parse — Parse a string following a given rule set
- bbcode_set_arg_parser — Attach another parser in order to use another rule set for argument parsing
- bbcode_set_flags — Set or alter parser options
BBCode
rothenbergxxx at gmail dot com
10-May-2009 09:22
10-May-2009 09:22
