以前写的一段 PHP UBB 函数发表评论!
搬家之前赖电脑面前想整理整理,不想翻出来个以前写的 PHP UBB 函数,额……忘记当初是因为什么来写这个了,只记得这个好像不怎么全,哦对了,没有 list 和 media 部分。管它了,贴出来也好警醒下自己继续完善,不要又封尘了。
<?php
/*
-----===== PHP 版 UBB 代码转换器 =====-----
----- Author:Wady -----
-----===== Update:2007-8-31 =====-----
*/
function ubbcode($strContent, $DisIMG, $DisUBB, $AutoURL) {
if( empty($strContent) ){
return $strContent;
}
//代码标签
$pattern = "/\[code\](\s\n|\n|)(.[^\[]*)\[\/code\](\s\n|\n|)/ie";
$replacement = "'<blockquote>'.checkStr('\$2', 'code').'</blockquote>'";
$strContent = preg_replace($pattern, $replacement, $strContent);
//链接自动转换
if( $AutoURL == 1 ){
$pattern = "/([^\=\]][\s]*?|^)(http|https|rstp|ftp|mms|ed2k):\/\/(.*?)(\>|\<|\"\;|\>\;|\<\;|\)|\(|$| )/iem";
$replacement = "'\$1<a href=\"'.checkStr('\$2://\$3', 'url').'\">\$2://\$3</a>\$4'";
$strContent = preg_replace($pattern, $replacement, $strContent);
}
if( $DisUBB == 0 ){
//图片转换
if( $DisIMG == 0 ){
$pattern = "/\[img\](.[^\]]*)\[\/img\]/ie";
$replacement = "'<img src=\"'.checkStr('\$1', 'url').'\" border=\"0\" alt=\"\" />'";
$strContent = preg_replace($pattern, $replacement, $strContent);
$pattern = "/\[img=(left|right|center|absmiddle|)\](.[^\]]*)\[\/img\]/ie";
$replacement = "'<img align=\"\$1\" src=\"'.checkStr('\$2', 'url').'\" border=\"0\" alt=\"\" />'";
$strContent = preg_replace($pattern, $replacement, $strContent);
$pattern = "/\[img=(\d*|),(\d*|)\](.[^\]]*)\[\/img\]/ie";
$replacement = "'<img src=\"'.checkStr('\$3', 'url').'\" width=\"\$1px\" height=\"\$2px\" border=\"0\" alt=\"\" />'";
$strContent = preg_replace($pattern, $replacement, $strContent);
$pattern = "/\[img=(\d*|),(\d*|),(left|right|center|absmiddle|)\](.[^\]]*)\[\/img\]/ie";
$replacement = "'<img align=\"\$3\" src=\"'.checkStr('\$4', 'url').'\" width=\"\$1px\" height=\"\$2px\" border=\"0\" alt=\"\" />'";
$strContent = preg_replace($pattern, $replacement, $strContent);
}else{
$pattern = "/\[img([^\]]*)\]([^<>]*?)\[\/img\]/ie";
$replacement = "'<a href=\"'.checkStr('\$2', 'url').'\" title=\"点击查看\" />查看图片</a>'";
$strContent = preg_replace($pattern, $replacement, $strContent);
}
//链接转换
$pattern = "/\[url=(.[^\]]*)\](.[^\[]*)\[\/url=(self|blank|top|)\]/ie";
$replacement = "'<a href=\"'.checkStr('\$1', 'url').'\" target=\"_\$3\" title=\"\" />\$2</a>'";
$strContent = preg_replace($pattern, $replacement, $strContent);
$pattern = "/\[url=(.[^\]]*)\](.[^\[]*)\[\/url\]/ie";
$replacement = "'<a href=\"'.checkStr('\$1', 'url').'\" title=\"\" />\$2</a>'";
$strContent = preg_replace($pattern, $replacement, $strContent);
$pattern = "/\[url\](.[^\[]*)\[\/url=(self|blank|top|)\]/ie";
$replacement = "'<a href=\"'.checkStr('\$1', 'url').'\" target=\"_\$2\" title=\"\" />\$1</a>'";
$strContent = preg_replace($pattern, $replacement, $strContent);
$pattern = "/\[url\](.[^\[]*)\[\/url\]/ie";
$replacement = "'<a href=\"'.checkStr('\$1', 'url').'\" title=\"\" />\$1</a>'";
$strContent = preg_replace($pattern, $replacement, $strContent);
//其他标签
$pattern = "/\[email=(.[^\]]*)\](.[^\[]*)\[\/email\]/ie";
$replacement = "'<a href=\"mailto:'.checkStr('\$1', 'email').'\" title=\"\$2\" />\$2</a>'";
$strContent = preg_replace($pattern, $replacement, $strContent);
$pattern = "/\[email\](.[^\[]*)\[\/email\]/ie";
$replacement = "'<a href=\"mailto:'.checkStr('\$1', 'email').'\" title=\"\" />'.checkEmail('\$1').'</a>'";
$strContent = preg_replace($pattern, $replacement, $strContent);
$strContent = preg_replace('/\[align=(\w{4,6})\]([^\r]*?)\[\/align\]/i','<div align="$1">$2</div>', $strContent);
$strContent = preg_replace('/\[color=(#\w{3,10}|\w{3,10})\]([^\r]*?)\[\/color\]/i','<span style="color:$1">$2</span>', $strContent);
$strContent = preg_replace('/\[size=(\d{1,2})\]([^\r]*?)\[\/size\]/i','<span style="font-size:$1pt">$2</span>', $strContent);
$strContent = preg_replace('/\[font=([^\r]*?)\]([^\r]*?)\[\/font\]/ie',"'<span style=\"font-family:'.checkStr('\$1', 'other').'\">$2</span>'", $strContent);
$strContent = preg_replace('/\[b\]([^\r]*?)\[\/b\]/i','<strong>$1</strong>', $strContent);
$strContent = preg_replace('/\[i\]([^\r]*?)\[\/i\]/i','<i>$1</i>', $strContent);
$strContent = preg_replace('/\[u\]([^\r]*?)\[\/u\]/i','<u>$1</u>', $strContent);
$strContent = preg_replace('/\[s\]([^\r]*?)\[\/s\]/i','<s>$1</s>', $strContent);
$strContent = preg_replace('/\[sup\]([^\r]*?)\[\/sup\]/i','<sup>$1</sup>', $strContent);
$strContent = preg_replace('/\[sub\]([^\r]*?)\[\/sub\]/i','<sub>$1</sub>', $strContent);
}
//列表标签
$strContent = preg_replace('/\[(ul|ol)\](\s\n|)/i', '<$1>', $strContent);
$strContent = preg_replace('/\[(ul|ol)=(.[^\]]*)\](\s\n|)/ie', "'<$1 style=\"list-style-type:'.checkStr('\$1', 'other').'\">'", $strContent);
$strContent = preg_replace('/\[\*\](.[^\[]*)(\n|)/ie', "'<li>'.trim('\$1').'</li>'", $strContent);
$strContent = preg_replace('/\[\/(ul|ol)\](\s\n|)/i', '</$1>', $strContent);
//义转
$strContent = preg_replace('/\n/', '<br/>', $strContent);
$strContent = preg_replace('/\t/', ' ', $strContent);
$strContent = preg_replace('/ /', ' ', $strContent);
//还原
//$strContent = checkStr($strContent, 'restore');
return $strContent;
}
function checkStr($str, $type){
$str = trim($str);
$str = preg_replace('/(d)(ocument\.cookie)/i', '$1ocument cookie', $str);
$str = preg_replace('/(d)(ocument\.write)/i', '$1ocument write', $str);
$str = preg_replace('/(s)(cript:)/i', '$1cript ', $str);
$str = preg_replace('/(s)(cript)/i', '$1cript', $str);
$str = preg_replace('/(o)(bject)/i', '$1bject', $str);
$str = preg_replace('/(a)(pplet)/i', '$1pplet', $str);
$str = preg_replace('/(e)(mbed)/i', '$1mbed', $str);
if ($type == 'url'){
if (preg_match('/:\/\//i', $str)) {
return $str;
}else{
$str = 'http://'.$str;
}
}
if ($type == 'code'){
$str = str_replace('[', '', $str);
$str = str_replace(']', ", $str);
$str = str_replace('&', '&', $str);
$str = str_replace('\"', '"', $str);
$str = str_replace('\", "', $str);
$str = str_replace('<', '<', $str);
$str = str_replace('>', '>', $str);
$str = str_replace(':', ':', $str);
}
if ($type == 'restore'){
$str = str_replace('$[$', '[', $str);
$str = str_replace('$]$', ']', $str);
}
if ($type == 'show'){
$str = str_replace('<br/>', '<br/> ', $str);
$str = str_replace('<', '<', $str);
$str = str_replace('>', '>', $str);
$str = str_replace('"', '"', $str);
$str = str_replace('> ', '><br/>', $str);
}
return $str;
}
?>

Soarcehiche
评论发表于 2008-06-07 17:28:27
It's good.