将BBCode代码解析成HTML

全屏阅读
  • 基本信息
  • 作者:
  • 作者已发布:924篇文章
  • 发布时间:2022年05月23日 23:55:11
  • 所属分类:Discuz
  • 阅读次数:1039次阅读
  • 标签:

函数示例

<?php 
    function bb_parse($string) { 
        $tags = 'b|i|size|color|center|quote|url|img'; 
        while (preg_match_all('/\[('.$tags.')=?(.*?)\](.+?)\[\/\1\]/s', $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</span>"; break; 
                case 'color': $replacement = "<span style=\"color: $param;\">$innertext</span>"; 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':
					$width = $height = '';
					if($param) 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; 
    }
	
	$text=<<<EOF
		[b]Bold Text[/b]
		[i]Italic Text[/i]
		[url]http://www.php.net/[/url]
		[url=http://pecl.php.net/][b]Content Text[/b][/url]
		[img]http://static.php.net/www.php.net/images/php.gif[/img]
		[url=http://www.php.net/]
		[img]http://static.php.net/www.php.net/images/php.gif[/img]
		[/url]
EOF;

	echo bb_parse($text);

运行后获得的是这样子的HTML

<strong>Bold Text</strong>
<em>Italic Text</em>
<a href="http://www.php.net/">http://www.php.net/</a>
<a href="http://pecl.php.net/"><strong>Content Text</strong></a>
<img src="http://static.php.net/www.php.net/images/php.gif" />
<a href="http://www.php.net/">
<img src="http://static.php.net/www.php.net/images/php.gif" />
</a>

顶一下
(0)
100%
订阅 回复
踩一下
(0)
100%
» 郑重声明:本文由mpxq168发布,所有内容仅代表个人观点。版权归恒富网mpxq168共有,欢迎转载, 但未经作者同意必须保留此段声明,并给出文章连接,否则保留追究法律责任的权利! 如果本文侵犯了您的权益,请留言。

目前有 0 条留言 其中:访客:0 条, 博主:0 条

给我留言

您必须 [ 登录 ] 才能发表留言!