Advertisement
carbonize

BBcode

Dec 18th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.91 KB | None | 0 0
  1. function bb_parse($string)
  2. {
  3.   $tags = 'b|i|size|color|center|quote|url|img|video';
  4.   while (preg_match_all('`\[('.$tags.')=?(.*?)\](.+?)\[/\1\]`', $string, $matches)) foreach ($matches[0] as $key => $match) {
  5.     list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]);
  6.     switch ($tag) {
  7.         case 'b': $replacement = "<strong>$innertext</strong>"; break;
  8.         case 'i': $replacement = "<em>$innertext</em>"; break;
  9.         case 'size': $replacement = "<span style=\"font-size: $param;\">$innertext</span>"; break;
  10.         case 'color': $replacement = "<span style=\"color: $param;\">$innertext</span>"; break;
  11.         case 'center': $replacement = "<div class=\"centered\">$innertext</div>"; break;
  12.         case 'quote': $replacement = "<blockquote>$innertext</blockquote>" . $param? "<cite>$param</cite>" : ''; break;
  13.         case 'url': $replacement = '<a href="' . ($param ? $param : $innertext) . "\">$innertext</a>"; break;
  14.         case 'img':
  15.           list($width, $height) = preg_split('`[Xx]`', $param);
  16.           $replacement = "<img src=\"$innertext\" " . (is_numeric($width)? "width=\"$width\" " : '') . (is_numeric($height)? "height=\"$height\" " : '') . '/>';
  17.         break;
  18.         case 'video':
  19.           $videourl = parse_url($innertext);
  20.           parse_str($videourl['query'], $videoquery);
  21.           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>';
  22.           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>';
  23.         break;
  24.     }
  25.     $string = str_replace($match, $replacement, $string);
  26.   }
  27.   return $string;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement