http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=10863以下是源代码
<?php
/************************************************************
* UBB 解析器 v1.0b
*
* 该解释器支持的是UBB的子集(标准集我也不知到啥样)
* 需要其他的可以自行扩展,
* 理论上支持任何[x=a,b,c]xxx[/x]结构的解析
*
* 源代码由zy提供,在此思路上重写了大部分代码
*
* 如果你有什么好的建议请联系 [email protected]
* 正则表达式的用法得到 [email protected] 的大力支持,在此表示感谢
* 耙子 2001/12/31
* http://www.fogsun.com
*
* $ 转载请完整保留此段文字 $
*
* 使用说明
* 1.本代码未对html进行任何限制,仅增加了对UBB-〉Html的转换支持,
* 如果需要请在调用本代码前自行过滤html标签,
* 2.调用代码前请用 stripslashes 函数去掉转换内容中的冗余反斜杠,
* 否则结果可能会出现问题
* 3.UBB 标签中不允许出现空格
*************************************************************/
define ("sVersion", "1.2.1.15 beta");
//ubbcode类
class ubbcode
{
var $nest; // 递归深度,for debug
//可处理标签及处理函数表
var $tags = array(
'url' => '$this->url',
'email' => '$this->email',
'mail' => '$this->email', // 为了容错,[mail]和[email]等效
'img' => '$this->img',
'b' => '$this->simple',
'i' => '$this->simple',
'u' => '$this->simple',
'tt' => '$this->simple',
's' => '$this->simple',
'strike' => '$this->simple',
'h1' => '$this->simple',
'h2' => '$this->simple',
'h3' => '$this->simple',
'h4' => '$this->simple',
'h5' => '$this->simple',
'h6' => '$this->simple',
'sup' => '$this->simple',
'sub' => '$this->simple',
'em' => '$this->simple',
'strong' => '$this->simple',
'code' => '$this->simple',
'samp' => '$this->simple',
'kbd' => '$this->simple',
'var' => '$this->simple',
'dfn' => '$this->simple',
'cite' => '$this->simple',
'small' => '$this->simple',
'big' => '$this->simple',
'blink' => '$this->simple',
'fly' => '$this->fly',
'move' => '$this->move',
'glow' => '$this->CSSStyle',
'shadow' => '$this->CSSStyle',
'blur' => '$this->CSSStyle',
'wave' => '$this->CSSStyle',
'sub' => '$this->simple',
'sup' => '$this->simple',
'size' => '$this->size',
'face' => '$this->face',
'font' => '$this->face', // 为了容错,[font]和[face]等效
'color' => '$this->color'
);
function ubbcode()
{
$this->$nest= 0;
$this->$sLastModified= sprintf("%s", date("Y-m-j H:i", getlastmod()));
}
/***********************************************************************
* 对使用者输入的 E-Mail 作简单的检查,
* 检查使用者的 E-Mail 字串是否有 @ 字元,
* 在 @ 字元前有英文字母或数字,在之后有数节字串,
* 最后的小数点后只能有二个或三个英文字母。
* [email protected] 就可以通过检查,[email protected] 就不能通过检查
************************************************************************/
function emailcheck($str)
{
if (eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $str))
return true;
else
return false;
}
/***********************************************************************
* 对使用者输入的 URL 作简单的检查,
* 目前只能简单判断,不能自动检查fpt,finger等
************************************************************************/
function checkURL($str)
{
$bValidURL= true;
if (eregi("([a-z0-9-]+([\.][a-z0-9\-]+)+)", $str, $er_arr))
{
/*
printf ("0. %s <br>\n", $er_arr[0]);
printf ("1. %s <br>\n", $er_arr[1]);
printf ("2. %s <br>\n", $er_arr[2]);
printf ("3. %s <br>\n", $er_arr[3]);
printf ("4. %s <br>\n", $er_arr[4]);
*/
}
else
$bValidURL= false;
return $bValidURL;
}
/***********************************************************************
* 对使用者输入的 图片URL 作简单的检查,
* 目前只能简单判断结尾是否为图片文件
* 不支持由CGI动态生成的图片,比如计数器这类的
************************************************************************/
function checkImgURL($str)
<?php
/************************************************************
* UBB 解析器 v1.0b
*
* 该解释器支持的是UBB的子集(标准集我也不知到啥样)
* 需要其他的可以自行扩展,
* 理论上支持任何[x=a,b,c]xxx[/x]结构的解析
*
* 源代码由zy提供,在此思路上重写了大部分代码
*
* 如果你有什么好的建议请联系 [email protected]
* 正则表达式的用法得到 [email protected] 的大力支持,在此表示感谢
* 耙子 2001/12/31
* http://www.fogsun.com
*
* $ 转载请完整保留此段文字 $
*
* 使用说明
* 1.本代码未对html进行任何限制,仅增加了对UBB-〉Html的转换支持,
* 如果需要请在调用本代码前自行过滤html标签,
* 2.调用代码前请用 stripslashes 函数去掉转换内容中的冗余反斜杠,
* 否则结果可能会出现问题
* 3.UBB 标签中不允许出现空格
*************************************************************/
define ("sVersion", "1.2.1.15 beta");
//ubbcode类
class ubbcode
{
var $nest; // 递归深度,for debug
//可处理标签及处理函数表
var $tags = array(
'url' => '$this->url',
'email' => '$this->email',
'mail' => '$this->email', // 为了容错,[mail]和[email]等效
'img' => '$this->img',
'b' => '$this->simple',
'i' => '$this->simple',
'u' => '$this->simple',
'tt' => '$this->simple',
's' => '$this->simple',
'strike' => '$this->simple',
'h1' => '$this->simple',
'h2' => '$this->simple',
'h3' => '$this->simple',
'h4' => '$this->simple',
'h5' => '$this->simple',
'h6' => '$this->simple',
'sup' => '$this->simple',
'sub' => '$this->simple',
'em' => '$this->simple',
'strong' => '$this->simple',
'code' => '$this->simple',
'samp' => '$this->simple',
'kbd' => '$this->simple',
'var' => '$this->simple',
'dfn' => '$this->simple',
'cite' => '$this->simple',
'small' => '$this->simple',
'big' => '$this->simple',
'blink' => '$this->simple',
'fly' => '$this->fly',
'move' => '$this->move',
'glow' => '$this->CSSStyle',
'shadow' => '$this->CSSStyle',
'blur' => '$this->CSSStyle',
'wave' => '$this->CSSStyle',
'sub' => '$this->simple',
'sup' => '$this->simple',
'size' => '$this->size',
'face' => '$this->face',
'font' => '$this->face', // 为了容错,[font]和[face]等效
'color' => '$this->color'
);
function ubbcode()
{
$this->$nest= 0;
$this->$sLastModified= sprintf("%s", date("Y-m-j H:i", getlastmod()));
}
/***********************************************************************
* 对使用者输入的 E-Mail 作简单的检查,
* 检查使用者的 E-Mail 字串是否有 @ 字元,
* 在 @ 字元前有英文字母或数字,在之后有数节字串,
* 最后的小数点后只能有二个或三个英文字母。
* [email protected] 就可以通过检查,[email protected] 就不能通过检查
************************************************************************/
function emailcheck($str)
{
if (eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $str))
return true;
else
return false;
}
/***********************************************************************
* 对使用者输入的 URL 作简单的检查,
* 目前只能简单判断,不能自动检查fpt,finger等
************************************************************************/
function checkURL($str)
{
$bValidURL= true;
if (eregi("([a-z0-9-]+([\.][a-z0-9\-]+)+)", $str, $er_arr))
{
/*
printf ("0. %s <br>\n", $er_arr[0]);
printf ("1. %s <br>\n", $er_arr[1]);
printf ("2. %s <br>\n", $er_arr[2]);
printf ("3. %s <br>\n", $er_arr[3]);
printf ("4. %s <br>\n", $er_arr[4]);
*/
}
else
$bValidURL= false;
return $bValidURL;
}
/***********************************************************************
* 对使用者输入的 图片URL 作简单的检查,
* 目前只能简单判断结尾是否为图片文件
* 不支持由CGI动态生成的图片,比如计数器这类的
************************************************************************/
function checkImgURL($str)
{
$Text=htmlspecialchars($Text);
$Text=stripslashes($Text);
$Text=ereg_replace("\r\n","<br/>",$Text);
$Text=ereg_replace("\r","<br/>",$Text);
$Text=nl2br($Text);
$Text=preg_replace("/\\t/is"," ",$Text);
$Text=ereg_replace(" "," ",$Text);
$Text=preg_replace("/\[h1\](.+?)\[\/h1\]/is","<h1>\\1</h1>",$Text);
$Text=preg_replace("/\[h2\](.+?)\[\/h2\]/is","<h2>\\1</h2>",$Text);
$Text=preg_replace("/\[h3\](.+?)\[\/h3\]/is","<h3>\\1</h3>",$Text);
$Text=preg_replace("/\[h4\](.+?)\[\/h4\]/is","<h4>\\1</h4>",$Text);
$Text=preg_replace("/\[h5\](.+?)\[\/h5\]/is","<h5>\\1</h5>",$Text);
$Text=preg_replace("/\[h6\](.+?)\[\/h6\]/is","<h6>\\1</h6>",$Text);
$Text=preg_replace("/\[center\](.+?)\[\/center\]/is","<center>\\1</center>",$Text);
$Text=preg_replace("/\[big\](.+?)\[\/big\]/is","<big>\\1</big>",$Text);
$Text=preg_replace("/\[small\](.+?)\[\/small\]/is","<small>\\1</small>",$Text);
$Text=preg_replace("/\[url\](http:\/\/.+?)\[\/url\]/is","<a href=\\1>\\1</a>",$Text);
$Text=preg_replace("/\[url\](.+?)\[\/url\]/is","<a href=\'http://\\1\'>http://\\1</a>",$Text);
$Text=preg_replace("/\[url=(http:\/\/.+?)\](.*)\[\/url\]/is","<a href=\\1>\\2</a>",$Text);
$Text=preg_replace("/\[url=(.+?)\](.*)\[\/url\]/is","<a href=http://\\1>\\2</a>",$Text);
$Text=preg_replace("/\[img\](.+?)\[\/img\]/is","<img src=\\1 border=0>",$Text);
$Text=preg_replace("/\[color=(.+?)\](.+?)\[\/color\]/is","<font color=\\1>\\2</font>",$Text);
$Text=preg_replace("/\[size=(.+?)\](.+?)\[\/size\]/is","<font size=\\1>\\2</font>",$Text);
$Text=preg_replace("/\[sup\](.+?)\[\/sup\]/is","<sup>\\1</sup>",$Text);
$Text=preg_replace("/\[sub\](.+?)\[\/sub\]/is","<sub>\\1</sub>",$Text);
$Text=preg_replace("/\[pre\](.+?)\[\/pre\]/is","<pre>\\1</pre>",$Text);
$Text=preg_replace("/\[email\](.+?)\[\/email\]/is","<a href=mailto:\\1>\\1</a>",$Text);
$Text=preg_replace("/\[i\](.+?)\[\/i\]/is","<i>\\1</i>",$Text);
$Text=preg_replace("/\[b\](.+?)\[\/b\]/is","<b>\\1</b>",$Text);
$Text=preg_replace("/\[quote\](.+?)\[\/quote\]/is","<blockquote><font size='1' face='Courier New'>quote:</font><hr>\\1<hr></blockquote>", $Text);
$Text=preg_replace("/\[code\](.+?)\[\/code\]/is","<blockquote><font size='1' face='Times New Roman'>code:</font><hr color='lightblue'><i>\\1</i><hr color='lightblue'></blockquote>", $Text);
$Text=preg_replace("/\[sig\](.+?)\[\/sig\]/is","<div style='text-align: left; color: darkgreen; margin-left: 5%'><br/><br/>--------------------------<br/>\\1<br/>--------------------------</div>", $Text);
$Text=ereg_replace("\[hr\]","<hr>",$Text);
return $Text;
}