note 90801 deleted from book.bbcode by danbrown

From: Date: Sun, 10 May 2009 21:38:41 +0000
Subject: note 90801 deleted from book.bbcode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154419@lists.php.net to get a copy of this message
Note Submitter: rothenbergxxx at gmail dot com ---- 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.

« previous php.notes (#154419) next »