(备忘)自己写的UBB2HTML与HTML2UBB函数(2 位领导批示)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
function ubb2html($str)
{
    $str = htmlspecialchars(stripslashes($str));
    // 字体颜色
    $str = preg_replace("#\[color\=([^\]]*)\]([^\[]*)#i","<span style='color:$1'>$2",$str);
    $str = preg_replace("#\[\/color\]#i","</span>",$str);
    // 字体大小
    $str = preg_replace("#\[size\=(\d)\]([^\[]*)#i","<font size='$1'>$2",$str);
    $str = preg_replace("#\[\/size\]#i","</font>",$str);
    // 超链接
    $str = preg_replace("#\[url\=([^\]]*)\]([^\[]*)#i","<a href='$1' target='_blank'>$2",$str);
    $str = preg_replace("#\[\/url\]#i","</a>",$str);
    // 图片
    $str = preg_replace("#\[img\]([^\[]*)\[\/img\]#i","<img src='$1' />",$str);
    // 其它
    $str = preg_replace("#\[([\/]?)b\]#i","<$1strong>",$str);
    $str = preg_replace("#\[([\/]?)i\]#i","<$1i>",$str);
    $str = preg_replace("#\[([\/]?)em\]#i","<$1em>",$str);
    $str = preg_replace("#\[([\/]?)u\]#i","<$1u>",$str);
    return nl2br($str);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
function html2ubb($str)
{
    $str = str_replace("& nbsp;",' ',$str);
    $str = str_replace("& mdash;",'—',$str);
    $str = str_replace("& quot;",'“',$str);
    $str = str_replace("<br />","\n\r",$str);
 
    $str = preg_replace("#\<a[^>]+href=\"([^\"]+)\"[^>]*\>(.*?)<\/a\>#i","[url=$1]$2[/url]",$str);
    $str = preg_replace("#\<font(.*?)color=\"#([^ >]+)\"(.*?)\>(.*?)<\/font\>#i","[color=$2]$4[/color]",$str);
    $str = preg_replace("#\<font(.*?)size=\"([^ >]+)\"(.*?)\>(.*?)<\/font\>#i","[size=$2]$4[/size]",$str);
    $str = preg_replace("#\<div[^>]+align=\"([^\"]+)\"[^>]*\>(.*?)<\/div\>#i","[align=$1]$2[/align]",$str);
    $str = preg_replace("#\<img[^>]+src=\"([^\"]+)\"[^>]*\>#i","[img]$1[/img]",$str);
    $str = preg_replace("#\<([\/]?)u\>#i","[$1u]",$str);
    $str = preg_replace("#\<([\/]?)em\>#i","[$1i]",$str);
    $str = preg_replace("#\<strong\>([^<]*)\<\/strong\>#i","[b]$1[/b]",$str);
    $str = preg_replace("#\<b\>([^<]*)\<\/b\>#i","[b]$1[/b]",$str);
    $str = preg_replace("#\<([\/]?)i\>#i","[$1i]",$str);
    $str = preg_replace("#<[^>]*?>#i","",$str);
    $str = addslashes($str);
    return $str;
}

分享到新浪微博 推荐到豆瓣 分享到 Google Reader 加入Google书签 嘀咕一下 分享到饭否 分享到做啥 分享到叽歪 分享到鲜果 加入QQ书签 加入百度搜藏 加入雅虎书签 分享到垦一垦

  1. 2
    银子

    @Dadait 当然可以 [face2]

  2. 1
    Dadait

    偶借来用用.可不可以哇.

请领导指示

验证码 (必须)

Additional comments powered by BackType

随机显示的10篇日志

评论最多的10篇日志

浏览最多的10篇日志