codetc - 网站开发技术 首页 代码 PHP 查看内容

PHP实现UBB代码转换

2015-6-28 13:46| 发布者: CODETC| 查看: 2526| 评论: 0

UBB代码是HTML的一个变种,是Ultimate Bulletin Board 采用的一种特殊的TAG。UBB代码很简单,功能很少,但是由于其Tag语法检查实现非常容易,所以不少网站引入了这种代码,以方便网友使用显示图片/链接/加粗字体等常见功能。


UBB代码使用正则表达式来进行匹配,不同的论坛所使用的UBB代码很可能不同,不能一概而论。UBB代码的出现,使得论坛可以使用类似HTML的标签来增加文字的属性,同时又不用害怕HTML代码中所夹带的不良信息!


下面是一个简单的PHP实现UBB代码转换的方法:

function ubb2html($content)
{
	global $article;
	//是否自动识别
	if ($article['isparseurl'] == "1")
	{
		$content = parseurl($content);
	}
	//自动识别结束
	$content = eregi_replace(quotemeta("[b]"),quotemeta("<b>"),$content);
	$content = eregi_replace(quotemeta("[/b]"),quotemeta("</b>"),$content);
	$content = eregi_replace(quotemeta("[i]"),quotemeta("<i>"),$content);
	$content = eregi_replace(quotemeta("[/i]"),quotemeta("</i>"),$content);
	$content = eregi_replace(quotemeta("[u]"),quotemeta("<u>"),$content);
	$content = eregi_replace(quotemeta("[/u]"),quotemeta("</u>"),$content);
	$content = eregi_replace(quotemeta("[center]"),quotemeta("<center>"),$content);
	$content = eregi_replace(quotemeta("[/center]"),quotemeta("</center>"),$content);
	$content = eregi_replace(quotemeta("[quote]"),quotemeta("<table width=\"96%\" border=\"0\" cellspacing=\"3\" cellpadding=\"0\" style=word-break:break-all align=\"center\"><tr><td><b>引用:</b></td></tr><tr><td><hr width=\"100%\" noshade></td></tr><tr><td class=\"content\"><font color=\"#0000FF\">"),$content);
	$content = eregi_replace(quotemeta("[/quote]"),quotemeta("</font></td></tr><tr><td><hr width=\"100%\" noshade></td></tr></table>"),$content);
	$content = eregi_replace(quotemeta("<div class="codetitle"><span><a style="CURSOR: pointer" data="96967" class="copybut" id="copybut96967" onclick="doCopy('code96967')"><u>复制代码</u></a></span> 代码如下:</div><div class="codebody" id="code96967">"),quotemeta("<table width=\"96%\" border=\"0\" cellspacing=\"3\" cellpadding=\"0\" style=word-break:break-all align=\"center\"><tr><td><b>代码:</b></td></tr><tr><td><hr width=\"100%\" noshade></td></tr><tr><td class=\"code\"><font color=\"#0000FF\">"),$content);
	$content = eregi_replace(quotemeta("</div>"),quotemeta("</font></td></tr><tr><td><hr width=\"100%\" noshade></td></tr></table>"),$content);
	$content = eregi_replace("\\[images\\]([^\\[]*)\\[/images\\]","<a href=\"\\1\" target=\"_blank\"><img src=\"\\1\" border=0 onload=\"javascript:if(this.width>screen.width-333)this.width=screen.width-333\" title=\"用新窗口浏览原始图片\"></a>",$content);
	$content = eregi_replace("\\[url\\]www.([^\\[]*)\\[/url\\]", "<a href=\"http://www.\\1\" target=_blank>www.\\1</a>",$content);
	$content = eregi_replace("\\[url\\]([^\\[]*)\\[/url\\]","<a href=\"\\1\" target=_blank>\\1</a>",$content);
	$content = eregi_replace("\\[url=([^\\[]*)\\]([^\\[]*)\\[/url\\]","<a href=\"\\1\" target=_blank>\\2</a>",$content);
	$content = eregi_replace("\\[email\\]([^\\[]*)\\[/email\\]", "<a href=\"mailto:\\1\">\\1</a>",$content);
	//$content = preg_replace( '/javascript/i', 'java script', $content);
	return $content;
}
文章来源 CODETC,欢迎分享,转载请注明地址: http://www.codetc.com/article-213-1.html

最新评论

 作为游客发表评论,请输入您的昵称

返回顶部