将BBCode代码解析成HTML

本文介绍了如何将BBCode代码解析成HTML,提到了PHP的BBCode扩展以及一个函数示例,帮助将 Bulletin Board Code 转换为HTML格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前两周写了将 smilies 转换成正常图片,本想贴一下BBCode的解析,然后今天看到了有 Bulletin Board Code 解析成HTML的扩展,也就懒的翻旧账了,哈哈。

扩展地址 http://pecl.php.net/package/bbcode

还有个函数示例:

<?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>

原函数在解析嵌套的BBCode时,解析不完整,所以稍微改了一下,其他的标签可以自己添加了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值