downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Introduction> <Text Processing
Last updated: Fri, 20 Nov 2009

view this page in

Bulletin Board Code



add a note add a note User Contributed Notes
BBCode
rothenbergxxx at gmail dot com
10-May-2009 09:22
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.

Introduction> <Text Processing
Last updated: Fri, 20 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites