note 90801 added to book.bbcode
| From: | rothenbergxxx at gmail dot com | Date: | Sun, 10 May 2009 19:22:00 +0000 |
| Subject: | note 90801 added to book.bbcode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154418@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 209.41.74.194
Probable Submitter: 65.96.115.178
----
Manual Page -- http://www.php.net/manual/en/book.bbcode.php
Edit -- https://master.php.net/note/edit/90801
Del: integrated -- https://master.php.net/note/delete/90801/integrated
Del: useless -- https://master.php.net/note/delete/90801/useless
Del: bad code -- https://master.php.net/note/delete/90801/bad+code
Del: spam -- https://master.php.net/note/delete/90801/spam
Del: non-english -- https://master.php.net/note/delete/90801/non-english
Del: in docs -- https://master.php.net/note/delete/90801/in+docs
Del: other reasons-- https://master.php.net/note/delete/90801
Reject -- https://master.php.net/note/reject/90801
Search -- https://master.php.net/manage/user-notes.php