什么是 UBB 标记

UBB 标记是您可能已经熟悉的 HTML标记的一种变种。基本上,它允许您在文章中添加本来需要 HTML 标记的功能或风格。即使您所在的讨论区不允许使用 HTML 标记,UBB 标记仍然可以使用。因为比 HTML 要求更少的编码技术,您也许更愿意使用 UBB 标记,即便讨论区允许使用 HTML 标记。

什么是 UBB 标记?

所谓UBB代码,是指论坛中的替代HTML代码的安全代码。

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

目前支持的 UBB 标记:

UltraBoard Code 说明
[url]www.bbs.com[/url] 加入简单的超级链接
[url=www.bbs.com]www.bbs.com[/url] 加入带有说明的超级链接
[url=http://www.bbs.com]www.bbs.com[/url] 加入带有说明的超级链接
[download=http://www.bbs.com/2.zip]下载[/download] 下载地址和说明
[email]blue_dream@netease.com[/email] 加入带有说明的电子邮件地址。
[img]Your image address[/img] 显示你的图像,但请记住,http://是必须的。
[b]粗体[/b] 粗体显示
[i]斜体[/i] 斜体显示
[sup]上标[/sup] 上标显示
[sub]下标[/sub] 下标显示
[center]居中[/center] 居中显示
[color=#0000FF]颜色[/color] 带有颜色的显示
[&] 显示符号 &
[quote]引用[/quote] 引用显示
[font=宋体]字体[/font] 字体
[size=3]字体大小[/size] 字体大小
[#f7f7f7]彩色[/#] 同[color]标记
[code]代码[/code] 代码显示
[html]调试代码[/html] 发帖后,对于中间的调试代码,可以点运行按钮,查看调试效果

注意:

  1. 不能在一个功能中混合使用 HTML 和 UBB 标记。同时 UBB 标记是大小写无关的 (因而,你可以使用 [URL] 或 [url]).
  2. 不正确的 UBB 标记用法:
    • 不支持嵌套。
    • [url=”www.bbs.com”]www.bbs.com[/url] : 等于号后加的内容不用加引号。
    • [url] www.my.com [/url] : 不要在标记和标记作用的文字之间加空格。
    • [email]home@my.com[email] : 结束标记必须包括正斜杠 ([/email])

PHP :HTML 转为 UBB

function ubb($Text) {      /// UBB代码转换
        //$Text=htmlspecialchars($Text);
        //$Text=ereg_replace("\r\n","<br>",$Text);
        $Text=ereg_replace("\[br\]","<br />",$Text);
        $Text=nl2br($Text);
        $Text=stripslashes($Text);
        $Text=preg_replace("/\\t/is"," ",$Text);
        $Text=preg_replace("/\[url\](http:\/\/.+?)\[\/url\]/is","<a href=\"\\1\" target=\"new\"><u>\\1</u></a>",$Text);
        $Text=preg_replace("/\[url\](.+?)\[\/url\]/is","<a href=\"http://\\1\" target=\"new\"><u>\\1</u></a>",$Text);
        $Text=preg_replace("/\[url=(http:\/\/.+?)\](.+?)\[\/url\]/is","<a href=\"\\1\" target=\"new\"><u>\\2</u></a>",$Text);
        $Text=preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is","<a href=\"http://\\1\" target=\"new\"><u>\\2</u></a>",$Text);
        $Text=preg_replace("/\[color=(.+?)\](.+?)\[\/color\]/is","<font color=\"\\1\">\\2</font>",$Text);
        $Text=preg_replace("/\[font=(.+?)\](.+?)\[\/font\]/is","<font face=\"\\1\">\\2</font>",$Text);
        $Text=preg_replace("/\[email=(.+?)\](.+?)\[\/email\]/is","<a href=\"mailto:\\1\"><u>\\2</u></a>",$Text);
        $Text=preg_replace("/\[email\](.+?)\[\/email\]/is","<a href=\"mailto:\\1\"><u>\\1</u></a>",$Text);
        $Text=preg_replace("/\[i\](.+?)\[\/i\]/is","<i>\\1</i>",$Text);
        $Text=preg_replace("/\[u\](.+?)\[\/u\]/is","<u>\\1</u>",$Text);
        $Text=preg_replace("/\[b\](.+?)\[\/b\]/is","<b>\\1</b>",$Text);
        $Text=preg_replace("/\[fly\](.+?)\[\/fly\]/is","<marquee width=\"98%\" behavior=\"alternate\" scrollamount=\"3\">\\1</marquee>",$Text);
        $Text=preg_replace("/\[move\](.+?)\[\/move\]/is","<marquee width=\"98%\" scrollamount=\"3\">\\1</marquee>",$Text);
        $Text=preg_replace("/\[shadow=([#0-9a-z]{1,10})\,([0-9]{1,3})\,([0-9]{1,2})\](.+?)\[\/shadow\]/is","<table width=\"*\"><tr><td style=\"filter:shadow(color=\\1, direction=\\2 ,strength=\\3)\">\\4</td></tr></table>",$Text);
        return $Text;
  }

JS :HTML 转为 UBB

function htmltoubb(str){
        str = str.replace(/<br[^>]*>/ig,'\n');
        str = str.replace(/<p[^>\/]*\/>/ig,'\n'); 
        str = str.replace(/\son[\w]{3,16}\s?=\s*([\'\"]).+?\1/ig,'');
        str = str.replace(/<hr[^>]*>/ig,'[hr]');
        str = str.replace(/<(sub|sup|u|strike|b|i|pre)>/ig,'[$1]');
        str = str.replace(/<\/(sub|sup|u|strike|b|i|pre)>/ig,'[/$1]');
        str = str.replace(/<(\/)?b>/ig,'[$1b]');
        str = str.replace(/<(\/)?em>/ig,'[$1i]');
        str = str.replace(/<(\/)?blockquote([^>]*)>/ig,'[$1blockquote]');
        str = str.replace(/<img[^>]*smile=\"(\d+)\"[^>]*>/ig,'[s:$1]');
        str = str.replace(/<img[^>]*src=[\'\"\s]*([^\s\'\"]+)[^>]*>/ig,'[img]'+'$1'+'[/img]');

        str = str.replace(/<a[^>]*href=[\'\"\s]*([^\s\'\"]*)[^>]*>(.+?)<\/a>/ig,'[url]'+'$1'+'[/url]');
        str = str.replace(/<h([1-6])+>(.+?)<\/h\1>/ig,'[h$1]'+'$2'+'[/h$1]');
        str = str.replace(/<[^>]*?>/ig, '');
        str = str.replace(/&amp;/ig, '&');
        str = str.replace(/&lt;/ig, '<');
        str = str.replace(/&gt;/ig, '>');
}

参考文献:
http://www.blueidea.com/bbs/ubbcode.htm
https://www.cnblogs.com/theblueberry/p/3866224.html

1000

GS

北京 | php攻城狮

创作 35 粉丝 2

fighting