PHP 将 HTML 代码 转换到 UBB 论坛代码
程序员文章站
2024-01-09 11:44:34
...
烈16火84网(LieHuo24.Net)教17程 最近给下载栏目增设一键转帖功能,烈火下载栏目是使用DedeCMS构建的,大家都知道dede没有一键转帖功能,于是只好自己写一个,最终实现了。这里记下其中软件简介的HTML转UBB代码。
$intro = str_replace("\n",'',$intro);
$intro = preg_replace("/\]+HREF=\"([^\"]+)\"[^>]*\>(.*?)/i","[url=$1]$2[/url]",$intro);
$intro = preg_replace("/\]+)\"(.*?)\>(.*?)/i","[color=$2]$4[/color]",$intro);
$intro = preg_replace("/\]+)\"(.*?)\>(.*?)/i","[face=$2]$4[/face]",$intro);
$intro = preg_replace("/\]+)\"(.*?)\>(.*?)/i","[size=$2]$4[/size]",$intro);
$intro = preg_replace("/\]+src=\"([^\"]+)\"[^>]*\>/i","[img]$1[/img]",$intro);
$intro = preg_replace("/\
一键转帖演示:
关于PHP将HTML转UBB代码的问题,笔者在网上找到几个,但都不太好用,于是自己进行了修改和整理,如您有其它需求,可在下面留言。
PHP代码:
Copy to Clipboard引用的内容:[www.bkjia.com] $intro = $liehuo-net; // 这里是你获取的变量值。$intro = str_replace("\n",'',$intro);
$intro = preg_replace("/\]+HREF=\"([^\"]+)\"[^>]*\>(.*?)/i","[url=$1]$2[/url]",$intro);
$intro = preg_replace("/\]+)\"(.*?)\>(.*?)/i","[color=$2]$4[/color]",$intro);
$intro = preg_replace("/\]+)\"(.*?)\>(.*?)/i","[face=$2]$4[/face]",$intro);
$intro = preg_replace("/\]+)\"(.*?)\>(.*?)/i","[size=$2]$4[/size]",$intro);
$intro = preg_replace("/\]+src=\"([^\"]+)\"[^>]*\>/i","[img]$1[/img]",$intro);
$intro = preg_replace("/\
]+ALIGN=\"([^\"]+)\"[^>]*\>(.*?)/i","[align=$1]$2[/align]",$intro);
$intro = preg_replace("/\
$match = array(
"%\[b\](.*?)\[\/b\]%si",
"%\[center\](.*?)\[\/center\]%si",
"%\[url\](.*?)\[\/url\]%si",
"%\[url=(.*?)\](.*?)\[\/url\]%si",
"%\[img\](.*?)\[\/img\]%si",
"%\[fieldset=(.*?)\](.*?)\[\/fieldset\]%si"
);
$replace = array(
"$1",
"$1 ",
"$1",
"$2",
"550)this.width=500\" title=\"烈火提示:点击查看\">",
""
);
$strCodes = preg_replace($match, $replace, $strCodes);
return $strCodes ;
}
?>
{
$sHtml=$sUBB;
$sHtml=preg_replace("/",'$sHtml=preg_replace("/>/",'>',$sHtml);
$sHtml=preg_replace("/\r?\n/",'
',$sHtml);
$sHtml=preg_replace("/\[(\/?)(b|u|i|s|sup|sub)\]/i",'',$sHtml);
$sHtml=preg_replace("/\[color\s*=\s*([^\]]+?)\]/i",'',$sHtml);
function getSizeName($match)
{
$arrSize=array('8pt','10pt','12pt','14pt','18pt','24pt','36pt');
return '';
}
$sHtml=preg_replace_callback("/\[size\s*=\s*(\d+?)\]/i",'getSizeName',$sHtml);
$sHtml=preg_replace("/\[font\s*=\s*([^\]]+?)\]/i",'',$sHtml);
$sHtml=preg_replace("/\[back\s*=\s*([^\]]+?)\]/i",'',$sHtml);
$sHtml=preg_replace("/\[\/(color|size|font|back)\]/i",'',$sHtml);
for($i=0;$i$2',$sHtml);
$sHtml=preg_replace("/\[img\]\s*([\s\S]+?)\s*\[\/img\]/i",'',$sHtml);
function getImg($match)
{
$p1=$match[1];$p2=$match[2];$p3=$match[3];$src=$match[4]; title="烈火提示:点击查看"
$a=$p3?$p3:($p2?$p1:'');
return '';
}
$sHtml=preg_replace_callback("/\[img\s*=(?:\s*(\d+)\s*,\s*(\d+)\s*)?(?:,?\s*(\w+)\s*)?\]\s*([\s\S]+?)\s*\[\/img\]/i",'getImg',$sHtml);
$sHtml=preg_replace("/\[url\]\s*([\s\S]+?)\s*\[\/url\]/i",'$1',$sHtml);
$sHtml=preg_replace("/\[url\s*=\s*([^\]\s]+?)\s*\]\s*([\s\S]+?)\s*\[\/url\]/i",'$2',$sHtml);
$sHtml=preg_replace("/\[email\]\s*([\s\S]+?)\s*\[\/email\]/i",'$1',$sHtml);
$sHtml=preg_replace("/\[email\s*=\s*([^\]\s]+?)\s*\]\s*([\s\S]+?)\s*\[\/email\]/i",'$2',$sHtml);
$sHtml=preg_replace("/\[quote\]([\s\S]*?)\[\/quote\]/i",'
$sHtml=preg_replace("/\[code\]([\s\S]*?)\[\/code\]/i",'
function getFlash($match)
{
$w=$match[1];$h=$match[2];$url=$match[3];
if(!$w)$w=550;if(!$h)$h=400;
return '';
}
$sHtml=preg_replace_callback("/\[flash\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*)?\]([\s\S]+?)\[\/flash\]/i",'getFlash',$sHtml);
function getMedia($match)
{
$w=$match[1];$h=$match[2];$play=$match[3];$url=$match[4];
if(!$w)$w=550;if(!$h)$h=400;
return '';
}
$sHtml=preg_replace_callback("/\[media\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d+)\s*)?)?\]([\s\S]+?)\[\/media\]/i",'getMedia',$sHtml);
function getTable($match)
{
$w=$match[1];$b=$match[2];
$str='
if($w)$str.=' width="'.$w.'"';
if($b)$str.=' bgcolor="'.$b.'"';
return $str.'>';
}
$sHtml=preg_replace_callback("/\[table(?:\s*=\s*(\d{1,4}%?)\s*(?:,\s*([^\]]+)\s*)?)?]/i",'getTable',$sHtml);
function getTR($match){return '';}
$sHtml=preg_replace_callback("/\[tr(?:\s*=(\s*[^\]]+))?\]/i",'getTR',$sHtml);
function getTD($match){
$col=$match[1];$row=$match[2];$w=$match[3];
return '1?' colspan="'.$col.'"':'').($row>1?' rowspan="'.$row.'"':'').($w?' width="'.$w.'"':'').'>';
}
$sHtml=preg_replace_callback("/\[td(?:\s*=\s*(\d{1,2})\s*,\s*(\d{1,2})\s*(?:,\s*(\d{1,4}%?))?)?\]/i",'getTD',$sHtml);
$sHtml=preg_replace("/\[\/(table|tr|td)\]/i",'$1>',$sHtml);
$sHtml=preg_replace("/\[\*\]([^\[]+)/i",'$1 ',$sHtml);
function getUL($match)
{
$str='
return $str.'>';
}
$sHtml=preg_replace_callback("/\[list(?:\s*=\s*([^\]]+)\s*)?\]/i",'getUL',$sHtml);
$sHtml=preg_replace("/\[\/list\]/i",'',$sHtml);
return $sHtml;
}
?>
$intro = preg_replace("/\
]+ALIGN=\"([^\"]+)\"[^>]*\>(.*?)/i","[align=$1]$2[/align]",$intro);
$intro = preg_replace("/\/i","[$1u]",$intro);
$intro = preg_replace("/\/i","[$1I]",$intro);
$intro = preg_replace("/\/i","[$1b]",$intro);
$intro = preg_replace("/\/i","[$1i]",$intro);
echo $intro; // 最后得到UBB代码
?>
现在我们知道了HTML到UBB的转换,那么UBB是否能返回HTML呢?请参照下面的代码,网上收集的。
代码一:
Copy to Clipboard引用的内容:[www.bkjia.com] function ubb2html($strCodes) {$match = array(
"%\[b\](.*?)\[\/b\]%si",
"%\[center\](.*?)\[\/center\]%si",
"%\[url\](.*?)\[\/url\]%si",
"%\[url=(.*?)\](.*?)\[\/url\]%si",
"%\[img\](.*?)\[\/img\]%si",
"%\[fieldset=(.*?)\](.*?)\[\/fieldset\]%si"
);
$replace = array(
"$1",
"
"$1",
"$2",
"550)this.width=500\" title=\"烈火提示:点击查看\">",
""
);
$strCodes = preg_replace($match, $replace, $strCodes);
return $strCodes ;
}
?>
代码二:
Copy to Clipboard引用的内容:[www.bkjia.com] function ubb2html($sUBB){
$sHtml=$sUBB;
$sHtml=preg_replace("/",'$sHtml=preg_replace("/>/",'>',$sHtml);
$sHtml=preg_replace("/\r?\n/",'
',$sHtml);
$sHtml=preg_replace("/\[(\/?)(b|u|i|s|sup|sub)\]/i",'',$sHtml);
$sHtml=preg_replace("/\[color\s*=\s*([^\]]+?)\]/i",'',$sHtml);
function getSizeName($match)
{
$arrSize=array('8pt','10pt','12pt','14pt','18pt','24pt','36pt');
return '';
}
$sHtml=preg_replace_callback("/\[size\s*=\s*(\d+?)\]/i",'getSizeName',$sHtml);
$sHtml=preg_replace("/\[font\s*=\s*([^\]]+?)\]/i",'',$sHtml);
$sHtml=preg_replace("/\[back\s*=\s*([^\]]+?)\]/i",'',$sHtml);
$sHtml=preg_replace("/\[\/(color|size|font|back)\]/i",'',$sHtml);
for($i=0;$i$2',$sHtml);
$sHtml=preg_replace("/\[img\]\s*([\s\S]+?)\s*\[\/img\]/i",'',$sHtml);
function getImg($match)
{
$p1=$match[1];$p2=$match[2];$p3=$match[3];$src=$match[4]; title="烈火提示:点击查看"
$a=$p3?$p3:($p2?$p1:'');
return '';
}
$sHtml=preg_replace_callback("/\[img\s*=(?:\s*(\d+)\s*,\s*(\d+)\s*)?(?:,?\s*(\w+)\s*)?\]\s*([\s\S]+?)\s*\[\/img\]/i",'getImg',$sHtml);
$sHtml=preg_replace("/\[url\]\s*([\s\S]+?)\s*\[\/url\]/i",'$1',$sHtml);
$sHtml=preg_replace("/\[url\s*=\s*([^\]\s]+?)\s*\]\s*([\s\S]+?)\s*\[\/url\]/i",'$2',$sHtml);
$sHtml=preg_replace("/\[email\]\s*([\s\S]+?)\s*\[\/email\]/i",'$1',$sHtml);
$sHtml=preg_replace("/\[email\s*=\s*([^\]\s]+?)\s*\]\s*([\s\S]+?)\s*\[\/email\]/i",'$2',$sHtml);
$sHtml=preg_replace("/\[quote\]([\s\S]*?)\[\/quote\]/i",'
$1',$sHtml);
$sHtml=preg_replace("/\[code\]([\s\S]*?)\[\/code\]/i",'
$1
',$sHtml); function getFlash($match)
{
$w=$match[1];$h=$match[2];$url=$match[3];
if(!$w)$w=550;if(!$h)$h=400;
return '';
}
$sHtml=preg_replace_callback("/\[flash\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*)?\]([\s\S]+?)\[\/flash\]/i",'getFlash',$sHtml);
function getMedia($match)
{
$w=$match[1];$h=$match[2];$play=$match[3];$url=$match[4];
if(!$w)$w=550;if(!$h)$h=400;
return '';
}
$sHtml=preg_replace_callback("/\[media\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d+)\s*)?)?\]([\s\S]+?)\[\/media\]/i",'getMedia',$sHtml);
function getTable($match)
{
$w=$match[1];$b=$match[2];
$str='
if($b)$str.=' bgcolor="'.$b.'"';
return $str.'>';
}
$sHtml=preg_replace_callback("/\[table(?:\s*=\s*(\d{1,4}%?)\s*(?:,\s*([^\]]+)\s*)?)?]/i",'getTable',$sHtml);
function getTR($match){return '
$sHtml=preg_replace_callback("/\[tr(?:\s*=(\s*[^\]]+))?\]/i",'getTR',$sHtml);
function getTD($match){
$col=$match[1];$row=$match[2];$w=$match[3];
return '
}
$sHtml=preg_replace_callback("/\[td(?:\s*=\s*(\d{1,2})\s*,\s*(\d{1,2})\s*(?:,\s*(\d{1,4}%?))?)?\]/i",'getTD',$sHtml);
$sHtml=preg_replace("/\[\/(table|tr|td)\]/i",'$1>',$sHtml);
$sHtml=preg_replace("/\[\*\]([^\[]+)/i",'
function getUL($match)
{
$str='
return $str.'>';
}
$sHtml=preg_replace_callback("/\[list(?:\s*=\s*([^\]]+)\s*)?\]/i",'getUL',$sHtml);
$sHtml=preg_replace("/\[\/list\]/i",'',$sHtml);
return $sHtml;
}
?>
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
相关文章
相关视频
专题推荐
-
独孤九贱-php全栈开发教程
全栈 170W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
-
玉女心经-web前端开发教程
入门 80W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
-
天龙八部-实战开发教程
实战 120W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习
- 最新文章
- 热门排行
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论