![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
这些代码的年头在一年以上了 一直存在一些问题 后来忙了 就没再去鸟它
设计的是用一个小型的模板来扩展ubb 现在看起来很幼稚
<?php
if(!defined("IN_ARTSYS")){
die("Include Error: not in the artsys!");
}
class UbbArgument{
var $itsUbbArg;
var $itsFilter;
var $itsValue;
var $itsHtmlTpl; //htmlcode...{:=$itsUbbArg}htmlcode...
//constructor
function UbbArgument () {
$this->itsUbbArg = "";
$this->itsFilter = "";
$this->itsValue = "";
$this->itsHtmlTpl = "";
}
//setters & getters
function SetUbbArg ($str) {
$this->itsUbbArg = $str;
}
function SetFilter ($str) {
$this->itsFilter = $str;
}
function SetValue ($str) {
if(1 == preg_match($this->itsFilter, $str, $arraySub)){
$this->itsValue = $str;
return $arraySub;
}
return false;
}
function SetHtmlTpl ($str) {
$this->itsHtmlTpl = substr($str, 3, -1);
}
function GetUbbArg () {
return $this->itsUbbArg;
}
function GetValue () {
return $this->itsValue;
}
function GetHtml () {
if("" == $this->itsHtmlTpl){
return false;
}
return "" == $this->itsValue ? "" : str_replace("{:=" . $this->itsUbbArg . "}", $this->itsValue, $this->itsHtmlTpl);
}
function GetHtmlTpl () {
return $this->itsHtmlTpl;
}
}
class UbbRelation{
var $itsUbbTag;
var $itsHtmlTpl;
var $itsFixArguments;
var $itsFloArguments;
var $itsString;
//constructor
function UbbRelation () {
$this->itsUbbTag = "";
$this->itsHtmlTpl = "";
$this->itsFixArguments = array();
$this->itsFloArguments = array();
$this->itsString = "";
}
//setters & getters
function Creater ($UbbTpl, $HtmlTpl) { //[head p1(filter) ...] html... (:=code{:=p1})
//分析html模板
preg_match_all("~\(:=.*?\{:=(\w+?)\}.*?\)~is", $HtmlTpl, $arrayHtml);
$this->itsHtmlTpl = $HtmlTpl;
//探测并筛选UBB标记头
if(1 != preg_match("~^\[(\w+)\s*(.*)\]$~is", $UbbTpl, $tmpArray)){
die("错误:UBB标记格式错误
");
}
$this->itsUbbTag = $tmpArray[1];
$arrayUP = array_unique(explode(" ", $tmpArray[2]));
if(!(1 == count($arrayUP) && "" == current($arrayUP))){
foreach($arrayUP as $UP){
preg_match("~^(_?\w+)\((.+)\)$~i", $UP, $tmpArray);
$theUbbArg = new UbbArgument();
$theUbbArg->SetFilter($tmpArray[2]);
if("_" == $tmpArray[1][0]){
$theUbbArg->SetUbbArg(substr($tmpArray[1], 1));
$key = array_search(substr($tmpArray[1], 1), $arrayHtml[1]);
if(false === $key){
die("错误:UBB标记在HTML模板中未找到
");
}else{
$theUbbArg->SetHtmlTpl($arrayHtml[0][$key]);
$this->AddFixArguments($theUbbArg);
}
}else{
$theUbbArg->SetUbbArg($tmpArray[1]);
$key = array_search($tmpArray[1], $arrayHtml[1]);
if(false === $key){
die("错误:UBB标记在HTML模板中未找到
");
}else{
$theUbbArg->SetHtmlTpl($arrayHtml[0][$key]);
$this->AddFloArguments($theUbbArg);
}
}
}
}
}
function AddFixArguments ($arg) { //UbbArgument object
if(array_key_exists($arg->GetUbbArg(), $this->itsFloArguments)){
return false;
}
$this->itsFixArguments[$arg->GetUbbArg()] = $arg;
return true;
}
function AddFloArguments ($arg) { //UbbArgument object
if(array_key_exists($arg->GetUbbArg(), $this->itsFixArguments)){
return false;
}
$this->itsFloArguments[$arg->GetUbbArg()] = $arg;
return true;
}
function GetUbbTag () {
return $this->itsUbbTag;
}
function SetString ($str) {
if(!is_string($str)){
die("Error: is not a string for SetString function on UbbRelation object");
}
$this->itsString = $str;
}
function GetHtml () {
$RegExp = "~^\[$this->itsUbbTag\s*(.*?)\](.*)\[\/$this->itsUbbTag\]$~is";
if(1 == preg_match($RegExp, $this->itsString, $tmpArray)){
$theArgs = array_unique(explode(" ", $tmpArray[1]));
$theContent = $tmpArray[2];
}
$argumentBuffer = "";
foreach($theArgs as $Arg){
if(1 == preg_match("~^(\w+)=(.+)~i", $Arg, $arrayTmp)){
$argumentBuffer = $arrayTmp[1];
if(isset($this->itsFixArguments[$arrayTmp[1]])){
$this->itsFixArguments[$arrayTmp[1]]->SetValue($arrayTmp[2]);
}elseif(isset($this->itsFloArguments[$arrayTmp[1]])){
$this->itsFloArguments[$arrayTmp[1]]->SetValue($arrayTmp[2]);
}
}else{
if(isset($this->itsFixArguments[$argumentBuffer])){
$this->itsFixArguments[$argumentBuffer]->SetValue($this->itsFixArguments[$argumentBuffer]->GetValue() . " $Arg");
}elseif(isset($this->itsFloArguments[$argumentBuffer])){
$this->itsFloArguments[$argumentBuffer]->SetValue($this->itsFloArguments[$argumentBuffer]->GetValue() . " $Arg");
}
}
}
$tmpStr = $this->itsHtmlTpl;
foreach($this->itsFixArguments as $argument){
if(false === $argument->GetHtml()){
return false;
}
$tmpStr = str_replace("(:=" . $argument->GetHtmlTpl() . ")", $argument->GetHtml(), $tmpStr);
}
foreach($this->itsFloArguments as $argument){
if(false === $argument->GetHtml()){
return false;
}
$tmpStr = str_replace("(:=" . $argument->GetHtmlTpl() . ")", $argument->GetHtml(), $tmpStr);
}
return str_replace("[:=content]", $theContent, $tmpStr);
}
function GetFiltUbb () {
$RegExp = "~^\[$this->itsUbbTag\s*(?:.*?)\](.*)\[\/$this->itsUbbTag\]$~is";
return preg_replace($RegExp, "\\1", $this->itsString);
}
}
class Ubb2Html{
var $itsOriginal;
var $itsRelation;
var $itsCloseRela; //禁用的UBB标签关系
var $itsCharset;
//constructor
function Ubb2Html ($str = "") {
$this->itsOriginal = $str;
$this->itsRelation = array();
$this->itsCharset = "gb2312";
$this->itsCloseRela = array();
}
//Setters & Getters
function SetOriginal ($str) {
$this->itsOriginal = $str;
}
function SetCharset ($str) {
$this->itsCharset = $str;
}
function AddCloseRela ($str) {
if(is_string($str) && array_key_exists($str, $this->itsRelation)){
array_push($this->itsCloseRela, $str);
}else{
return false;
}
}
function AddRelation ($relation) {
$this->itsRelation[$relation->GetUbbTag()] = $relation;
}
function AddCreatRela ($UbbTpl, $HtmlTpl) {
$tmpRelation = new UbbRelation();
$tmpRelation->Creater($UbbTpl, $HtmlTpl);
$this->AddRelation($tmpRelation);
}
function GetHtml () {
foreach($this->itsCloseRela as $close){
unset($this->itsRelation[$close]);
}
$tmpStr = $this->itsOriginal;
$arrayUbbName = array_keys($this->itsRelation);
$UbbRegExp = "~^(.*?)(\[" . implode("\s*?.*?\]|\[", $arrayUbbName) . "\s*.*?\])(.*)$~is";
$UbbHeadRegExp = "~^\[(" . implode("|", $arrayUbbName) . ")\s*.*?\]$~is";
$arrayBefor = array();
$arrayUbbTag = array();
$strUbbResult = ""; //ubb结果
$ubbTagResult = array(); //标注以编译的UBB标签
//分割UBB标记
$i = 0;
for(;1;){
if(1 != preg_match($UbbRegExp, $tmpStr, $arrayTmp)){
break;
}
$arrayBefor[$i] = $arrayTmp[1];
$arrayUbbTag[$i] = $arrayTmp[2];
$tmpStr = $arrayTmp[3];
++ $i;
}
$arrayBefor[$i] = $tmpStr;
//逆向匹配UBB标记
for($j = $i - 1;$j >= 0;$j --){
preg_match($UbbHeadRegExp, $arrayUbbTag[$j], $arrayTmp); //获取Ubb标记头
arrayTmp[1]
$offset = strtolower($arrayTmp[1]);
for($k = $i;$k > $j;$k --){
if(1 == preg_match("~^(.*?)\[\/" . $arrayTmp[1] . "\](.*)$~is", $arrayBefor[$k], $arrayTie)){ //是否找到Ubb标签尾
for($l = $j + 1;$l < $k;$l ++){
$strUbbTag = in_array($l, $ubbTagResult) ? $arrayUbbTag[$l] : htmlspecialchars($arrayUbbTag[$l], ENT_QUOTES, $this->itsCharset);
$strUbbResult .= htmlspecialchars($arrayBefor[$l], ENT_QUOTES, $this->itsCharset) . $strUbbTag;
$arrayBefor[$l] = "";
$arrayUbbTag[$l] = "";
}
$combinedString = $arrayUbbTag[$j] . $strUbbResult . htmlspecialchars($arrayTie[1], ENT_QUOTES, $this->itsCharset) . "[/{$arrayTmp[1]}]";
$this->itsRelation[$offset]->SetString($combinedString);
$strUbbResult = ""; //清空
$htmlResult = $this->itsRelation[$offset]->GetHtml();
$arrayUbbTag[$j] = (false === $htmlResult) ? $combinedString : $htmlResult;
array_push($ubbTagResult, $j);
$arrayBefor[$k] = $arrayTie[2];
}
}
}
//组合结果字符串
$tmpStr = "";
foreach($arrayUbbTag as $ele){
$tmpStr .= array_shift($arrayBefor) . $ele;
}
return $tmpStr .= array_shift($arrayBefor);
}
function GetFiltUbb () {
foreach($this->itsCloseRela as $close){
unset($this->itsRelation[$close]);
}
$tmpStr = $this->itsOriginal;
$arrayUbbName = array_keys($this->itsRelation);
$UbbRegExp = "~^(.*?)(\[" . implode("\s*?.*?\]|\[", $arrayUbbName) . "\s*.*?\])(.*)$~i";
$UbbHeadRegExp = "~^\[(" . implode("|", $arrayUbbName) . ")\s*.*?\]$~i";
$arrayBefor = array();
$arrayUbbTag = array();
$strUbbResult = ""; //ubb结果
$ubbTagResult = array(); //标注以编译的UBB标签
//分割UBB标记
$i = 0;
for(;1;){
if(1 != preg_match($UbbRegExp, $tmpStr, $arrayTmp)){
break;
}
$arrayBefor[$i] = $arrayTmp[1];
$arrayUbbTag[$i] = $arrayTmp[2];
$tmpStr = $arrayTmp[3];
++ $i;
}
$arrayBefor[$i] = $tmpStr;
//逆向匹配UBB标记
for($j = $i - 1;$j >= 0;$j --){
preg_match($UbbHeadRegExp, $arrayUbbTag[$j], $arrayTmp); //获取Ubb标记头
arrayTmp[1]
$offset = strtolower($arrayTmp[1]);
for($k = $i;$k > $j;$k --){
if(1 == preg_match("~^(.*?)\[\/" . $arrayTmp[1] . "\](.*)$~i", $arrayBefor[$k], $arrayTie)){ //是否找到Ubb标签尾
for($l = $j + 1;$l < $k;$l ++){
$strUbbTag = in_array($l, $ubbTagResult) ? $arrayUbbTag[$l] : htmlspecialchars($arrayUbbTag[$l], ENT_QUOTES, $this->itsCharset);
$strUbbResult .= htmlspecialchars($arrayBefor[$l], ENT_QUOTES, $this->itsCharset) . $strUbbTag;
$arrayBefor[$l] = "";
$arrayUbbTag[$l] = "";
}
$combinedString = $arrayUbbTag[$j] . $strUbbResult . htmlspecialchars($arrayTie[1], ENT_QUOTES, $this->itsCharset) . "[/{$arrayTmp[1]}]";
$this->itsRelation[$offset]->SetString($combinedString);
$strUbbResult = ""; //清空
$htmlResult = $this->itsRelation[$offset]->GetFilteUbb();
$arrayUbbTag[$j] = (false === $htmlResult) ? $combinedString : $htmlResult;
array_push($ubbTagResult, $j);
$arrayBefor[$k] = $arrayTie[2];
}
}
}
//组合结果字符串
$tmpStr = "";
foreach($arrayUbbTag as $ele){
$tmpStr .= array_shift($arrayBefor) . $ele;
}
return $tmpStr .= array_shift($arrayBefor);
}
function Translater ($string) {
$this->SetOriginal($string);
return $this->GetHtml();
}
}
?>

