note 93349 added to function.bbcode-create

From: Date: Fri, 04 Sep 2009 17:58:49 +0000
Subject: note 93349 added to function.bbcode-create
Groups: php.notes 
Request: Send a blank email to php-notes+get-160224@lists.php.net to get a copy of this message
For those without the BBCode extension, here's a relatively elegant function to do the trick. Keep in mind that if you're using XHTML and one of your users tries to overlap lags <b>Like <i>so</b></i>, it will invalidate your markup. Still working on an expression for this. <?php function bb_parse($string) { while (preg_match_all('\[(.+?)=?(.*?)\](.+?)\[/\1\]', $string, $matches)) foreach ($matches[0] as $key => $match) { list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]); 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($videourl['host'], 'youtube.com') !== FALSE) $replacement = '<embed src="http://www.youtube.com/v/' . $videoquery['v'] . '" type="application/x-shockwave-flash" width="425" height="344"></embed>'; if (strpos($videourl['host'], 'google.com') !== FALSE) $replacement = '<embed src="http://video.google.com/googleplayer.swf?docid=' . $videoquery['docid'] . '" width="400" height="326" type="application/x-shockwave-flash"></embed>'; break; } $string = str_replace($match, $replacement, $string); } return $string; } ?> [EDIT BY danbrown AT php DOT net: Contains a bugfix provided by (ramonvandam AT gmail DOT com) on 04-SEP-09 to address an improperly-defined parameter.] ---- Server IP: 69.147.83.197 Probable Submitter: 76.117.174.115 ---- Manual Page -- http://www.php.net/manual/en/function.bbcode-create.php Edit -- https://master.php.net/note/edit/93349 Del: integrated -- https://master.php.net/note/delete/93349/integrated Del: useless -- https://master.php.net/note/delete/93349/useless Del: bad code -- https://master.php.net/note/delete/93349/bad+code Del: spam -- https://master.php.net/note/delete/93349/spam Del: non-english -- https://master.php.net/note/delete/93349/non-english Del: in docs -- https://master.php.net/note/delete/93349/in+docs Del: other reasons-- https://master.php.net/note/delete/93349 Reject -- https://master.php.net/note/reject/93349 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#160224) next »