목차
[PHP]代码 " >[PHP]代码 
php教程 PHP源码 PHP写的UBB代码转换HTML代码

PHP写的UBB代码转换HTML代码

May 25, 2016 pm 05:11 PM
php

[PHP]代码 

<?php
/*

 * @author:zhangwj
 * @data:2012-01-10
 * @content:UBB Convert HTML
 * @email:zhangwj.9991.com@gmail.com

$eq = new EncodeQ3boy($str);初始化类

//以下为ubbEncode参数
$eq->url      = true;       //启用url自动解析   默认false
$eq->html     = true;       //启用HTML编码(处理<,>,全角/半角空格,制表符,换行符)默认true
$eq->image    = true;       //启用图象标签解析  默认true
$eq->font     = true;       //启用字体标签解析  默认true
$eq->element  = true;       //启用外部元素解析  默认true
$eq->flash    = true;       //启用Flash解析     默认true
$eq->php      = true;       //启用语法高亮显示  默认true
//ubbEncode参数结束

echo($eq->getImageOpener());//输出图片自动缩放所需js函数
echo $eq->htmlEncode();          //输出ubb编码后字符串
echo"<hr>";
echo $eq->ubbEncode();           //输出ubb编码后字符串
echo"<hr>";
echo $eq->removeHtml();          //输出移除html标签的字符串
echo"<hr>";
echo $eq->ubbEncode();           //输出移除ubb标签的字符串



支持ubb标签列表:

图片类:
[img]http://www.php.cn/[/img]    插入图片
[limg]http://www.php.cn/[/limg]  图片左绕排
[rimg]http://www.php.cn/[/rimg]  图片右绕排
[cimg]http://www.php.cn/[/cimg]  图片居中绕排

文本控制类:
[br] 换行符
[b]粗体字[b]
[i]斜体字[i]
[u]下划线[u]
[s]删除线[s]
[sub]文字下标[sub]
[sup]文字上标[sup]
[left]文字左对齐[left]
[right]文字右对齐[right]
[center]文字居中[center]
[align=(left|center|right)]文字对齐方式[align]
[size=([1-6])]文字大小[size]
[font=(字体)[font]
[color=(文字颜色)][color]
[list]无序列表[list]
[list=s]有序列表[list]
[list=(A|1|I)]有序列表(列表方式为(abc,123,I II III))[list]
[list=(num)]有序列表(自num开始计数)[list]
[li]列表单元项[li]

外部元素类:
[url]链接[/url]
[url=(链接)]链接文字[/url]
[email]邮件地址[/email]
[email=(邮件地址)]说明文字[/email]邮件地址
[quote]引用块[/quote]
[iframe]内插帧地址[/iframe]
[iframe=(内插帧高度)]内插帧地址[/iframe]
[swf]flash动画地址[/swf]
[swf=宽度,高度]flash动画地址[/swf]

代码块:
[code][/code]
[php][/php]
[code 代码块名称][/code]
[php 代码块名称][/php]

如需使用php语法高亮请务必在代码块两端加上"<??>"标签
*/

class EncodeQ3boy {
        var $str           = "";
        var $iconpath      = "/image/icon";//图标文件路径
        var $imagepath     = "/upfiles/article";//图片文件默认路径
        //var $tagfoot = &#39; border="1" onload="ImageLoad(this);" onClick="ImageOpen(this)" style="cursor: hand" &#39;;//图片文件附加属性
		var $tagfoot = &#39; border="1" style="cursor: hand" &#39;;//图片文件附加属性

        var $url     = false;        //url自动解析
        var $html    = true;                //HTML编码
        var $image   = true;                //解析图象标签
        var $font    = true;                //字体标签
        var $element = true;                //外部元素
        var $flash   = true;                //Flash
        var $php     = true;                //语法高亮显示
        var $others  = true;				//ubb转换时候的其他处理

        function EncodeQ3boy($str=&#39;&#39;,$imgph=&#39;&#39;) {
                if($str) {
                        $str = strtr($str,array("\n\r"=>"\n","\r\n"=>"\n","\r"=>"\n"," "=>" "));
                        $this->str = $str;
                }
                if($imgph) $this->imagepath = $imgph;
        }
        function getImageOpener() {
                return "<script language=\"javascript\" type=\"text/javascript\">\r\nfunction ImageLoad(img) {\r\nif(img.width>480) img.width=480;\r\n}\r\nfunction ImageOpen(img) {\r\nwindow.open(img.src,&#39;&#39;,&#39;menubar=no,scrollbars=yes,width=&#39;+(screen.width-8)+&#39;,height=&#39;+(screen.height-74)+&#39;,left=0,top=0&#39;);\r\n}\r\n</script>";
        }
        function removeHtml($str=&#39;&#39;) {
                if(!$str) $str = $this->str;
                return strip_tags($str);
        }
        function removeUbb($str=&#39;&#39;) {
                if(!$str) $str = $this->str;
                return preg_replace("/\[\/?\w+(\s+[^\]\s]+)*\s*\]/is","",$str);
        }
        function htmlEncode($str=&#39;&#39;) {
                if(!$str) $str = $this->str;
                $str = preg_replace("/\n{2,}/s","\n\n",$str);
                return str_replace("\n","\n<br />",$str);
        }
		function bbcodeurl($url, $tags) {
			if(!preg_match("/<.+?>/s", $url)) {
				return sprintf($tags, $url, addslashes($url));
			} else {
				return &#39; &#39;.$url;
			}
		}
		function parseimg($width, $height, $src) {
			return $this->bbcodeurl($src, &#39;<img&#39;.($width > 0 ? " width=\"$width\"" : &#39;&#39;).($height > 0 ? " height=\"$height\"" : &#39;&#39;)." src=\"$src\" border=\"0\" alt=\"\" />");
		}
        function ubbEncode($str=&#39;&#39;) {
                if(!$str) $str = $this->str;
                $rpl_ary = array();
                $reg_ary = array();
                if($this->html) $str = $this->htmlEncode($str,true);
                $tagfoot = $this->tagfoot;
                $icon    = $this->iconpath;
                $image   = $this->imagepath;
                if($this->php) {
                        preg_match_all(&#39;/(\n\<br \/\>)*\[(php|code)\s*(.*?)\]\s*(.+?)\s*\[\/(php|code)\](\n\<br \/\>)*/is&#39;,$str,$ary);
                        $str = preg_split(&#39;/(\n\<br \/\>)*\[(php|code)\s*(.*?)\]\s*(.+?)\s*\[\/(php|code)\](\n\<br \/\>)*/is&#39;,$str);
                }
                if($this->url){
                        $reg_ary = array_merge($reg_ary,array(
                                &#39;/(?<!\]|\=)\s*(\b\w+@(?:\w+\.)+\w{2,3})\b\s*(?<!\[)/i&#39;,
                                &#39;/(?<!\]|\=)\s*(\b(http|https|ftp):\/\/(\w+\.)+\w+(\/[\w|&|%|\?|=|\+|\.|-]+)*)\b\s*(?<!\[)/i&#39;,
                        ));
                        $rpl_ary = array_merge($rpl_ary,array(
                                &#39;[email]\\1[/email]&#39;,
                                &#39;[url]\\1[/url]&#39;,
                        ));
                }
                if($this->image) {
                        $reg_ary = array_merge($reg_ary,array(
                        "/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/is",
						"/\[img=(\d{1,4})[x|\,](\d{1,4})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/is",
                        &#39;/\[img\]\s*http(s?):\/\/(\S+)\s*\[\/img\]/i&#39;,//1
                        &#39;/\[limg\]\s*http(s?):\/\/(\S+)\s*\[\/limg\]/i&#39;,//2
                        &#39;/\[rimg\]\s*http(s?):\/\/(\S+)\s*\[\/rimg\]/i&#39;,//3
                        &#39;/\[cimg\]\s*http(s?):\/\/(\S+)\s*\[\/cimg\]/i&#39;,//4
                        &#39;/\[img\]\s*([^\/:]+)\s*\[\/img\]/i&#39;,//5
                        &#39;/\[limg\]\s*([^\/:]+)\s*\[\/limg\]/i&#39;,//6
                        &#39;/\[rimg\]\s*([^\/:]+)\s*\[\/rimg\]/i&#39;,//7
                        &#39;/\[cimg\]\s*([^\/:]+)\s*\[\/cimg\]/i&#39;,//8
                        &#39;/\[img\]\s*(\S+)\s*\[\/img\]/is&#39;,//9
                        &#39;/\[limg\]\s*(\S+)\s*\[\/limg\]/i&#39;,//10
                        &#39;/\[rimg\]\s*(\S+)\s*\[\/rimg\]/i&#39;,//11
                        &#39;/\[cimg\]\s*(\S+)\s*\[\/cimg\]/i&#39;,//12
                        ));
                        $rpl_ary = array_merge($rpl_ary,array(
                        $this->bbcodeurl(&#39;\\1&#39;, &#39;<img src="%s" &#39;.$tagfoot.&#39;/>&#39;),
						$this->parseimg(&#39;\\1&#39;, &#39;\\2&#39;, &#39;\\3&#39;),
                        &#39;<img src="http\1://\2"&#39;.$tagfoot.&#39;>&#39;,//1
                        &#39;<img src="http\1://\2"&#39;.$tagfoot.&#39; align="left">&#39;,//2
                        &#39;<img src="http\1://\2"&#39;.$tagfoot.&#39; align="right">&#39;,//3
                        &#39;<p align="center"><img src="http\1://\2"&#39;.$tagfoot.&#39;></p>&#39;,//4
                        &#39;<img src="&#39;.$image.&#39;/\1"&#39;.$tagfoot.&#39;>&#39;,//5
                        &#39;<img src="&#39;.$image.&#39;/\1"&#39;.$tagfoot.&#39; align="left">&#39;,//6
                        &#39;<img src="&#39;.$image.&#39;/\1"&#39;.$tagfoot.&#39; align="right">&#39;,//7
                        &#39;<p align="center"><img src="&#39;.$image.&#39;/\1"&#39;.$tagfoot.&#39;></p>&#39;,//8
                        &#39;<img src="\1"&#39;.$tagfoot.&#39;>&#39;,//9
                        &#39;<img src="\1"&#39;.$tagfoot.&#39; align="left">&#39;,//10
                        &#39;<img src="\1"&#39;.$tagfoot.&#39; align="right">&#39;,//11
                        &#39;<p align="center"><img src="\1"&#39;.$tagfoot.&#39;></p>&#39;,//12
                        ));
                }
                if($this->font) {
                        $reg_ary = array_merge($reg_ary,array(
                        &#39;/\[br\]/i&#39;,
                        &#39;/\[b\]/i&#39;,
                        &#39;/\[\/b\]/i&#39;,
                        &#39;/\[i(=s)?\]\s*(.+?)\s*\[\/i\]/is&#39;,
                        &#39;/\[u\]\s*(.+?)\s*\[\/u\]/is&#39;,
                        &#39;/\[s\]\s*(.+?)\s*\[\/s\]/is&#39;,
                        &#39;/\[sub\]\s*(.+?)\s*\[\/sub\]/is&#39;,
                        &#39;/\[sup\]\s*(.+?)\s*\[\/sup\]/is&#39;,
                        &#39;/\[left\]/i&#39;,
                        &#39;/\[\/left\]/i&#39;,
                        &#39;/\[right\]/i&#39;,
                        &#39;/\[\/right\]/i&#39;,
                        &#39;/\[center\]/i&#39;,
                        &#39;/\[\/center\]/i&#39;,
                        &#39;/\[align=\s*(left|center|right)\]/i&#39;,
                        &#39;/\[\/align\]/i&#39;,
                        &#39;/\[size=\s*([\.|\d])\s*\]/i&#39;,
                        &#39;/\[\/size\]/i&#39;,
                        &#39;/\[size=(\d+(\.\d+)?(px|pt|in|cm|mm|pc|em|ex|%)+?)\]/i&#39;,
                        &#39;/\[font=\s*(.*?)\s*\]/i&#39;,
                        &#39;/\[\/font\]/i&#39;,
                        &#39;/\[color=\s*(.*?)\s*\]/i&#39;,
                        &#39;/\[\/color\]/i&#39;,
                        &#39;/\[list\]/i&#39;,
                        &#39;/\[\/list\]/i&#39;,
                        &#39;/\[list=s\]/i&#39;,
                        &#39;/\[\/list\]/i&#39;,
                        &#39;/\[list=(A|1|I)\]/i&#39;,
                        &#39;/\[\/list\]/i&#39;,
                        &#39;/\[list=(\S+?)\]/i&#39;,
                        &#39;/\[\/list\]/i&#39;,
                        &#39;/\[li\]/i&#39;,
                        &#39;/\[\/li\]/i&#39;,
                        &#39;/\[p=(\d{1,2}), (\d{1,2}), (left|center|right)\]/i&#39;,
						&#39;/\[float=(left|right)\]/i&#39;
                        ));
                        $rpl_ary = array_merge($rpl_ary,array(
                        &#39;<br />&#39;,
                        &#39;<b>&#39;,
                        &#39;</b>&#39;,
                        &#39;<i>\\2</i>&#39;,
                        &#39;<u>\\1</u>&#39;,
                        &#39;<s>\\1</s>&#39;,
                        &#39;<sub>\\1</sub>&#39;,
                        &#39;<sup>\\1</sup>&#39;,
                        &#39;<p align="left">&#39;,
                        &#39;</p>&#39;,
                        &#39;<p align="right">&#39;,
                        &#39;</p>&#39;,
                        &#39;<p align="center">&#39;,
                        &#39;</p>&#39;,
                        &#39;<p align="\\1">&#39;,
                        &#39;</p>&#39;,
                        &#39;<font size="\\1pt">&#39;,
                        &#39;</font>&#39;,
                        &#39;<font style="font-size: \\1">&#39;,
                        &#39;<font face="\\1">&#39;,
                        &#39;</font>&#39;,
                        &#39;<font color="\\1">&#39;,
                        &#39;</font>&#39;,
                        &#39;<ul>&#39;,
                        &#39;</ul>&#39;,
                        &#39;<ol>&#39;,
                        &#39;</ol>&#39;,
                        &#39;<ol type="\\1">&#39;,
                        &#39;</ol>&#39;,
                        &#39;<ol start="\\1">&#39;,
                        &#39;</ol>&#39;,
                        &#39;<li>&#39;,
                        &#39;</li>&#39;,
                        &#39;<p style="line-height: \\1px; text-indent: \\2em; text-align: \\3;">&#39;,
						&#39;<span style="float: \\1;">&#39;
                        ));
                }
                if($this->element){
                        $reg_ary = array_merge($reg_ary,array(
                        &#39;/\[url=\s*(.+?)(,1)?\s*\]\s*(.+?)\s*\[\/url\]/i&#39;,
                        &#39;/\[url]\s*(.+?)\s*\[\/url\]/i&#39;,
                        &#39;/\[email=\s*(.+?)\s*\]\s*(.+?)\s*\[\/email\]/i&#39;,
                        &#39;/\[email]\s*(.+?)\s*\[\/email\]/i&#39;,
                        &#39;/\[quote\]\s*(<br \/>)?\s*(.+?)\s*\[\/quote\]/is&#39;,
                        &#39;/\[iframe\]\s*(.+?)\s*\[\/iframe\]/is&#39;,
                        &#39;/\[iframe=\s*(\d+?)\s*\]\s*(.+?)\s*\[\/iframe\]/is&#39;,
                        ));
                        $rpl_ary = array_merge($rpl_ary,array(
                        &#39;<a href="\1" target="_blank">\3</a> &#39;,
                        &#39;<a href="\1" target="_blank">\1</a> &#39;,
                        &#39;<a href="mailto:\1">\2</a> &#39;,
                        &#39;<a href="mailto:\1">\1</a> &#39;,
                        &#39;<table cellpadding="0" cellspacing="0" border="0" width="90%" align="center" style="border:1px gray solid;"><tr><td><table width="100%" cellpadding="5" cellspacing="1" border="0"><tr><td width="100%">\2</td></tr></table></td></tr></table>&#39;,
                        &#39;<iframe src="\" name="ifr1" frameborder="0" allowtransparency="true" scrolling="yes" width="100%" height="340" marginwidth="0" marginheight="0" hspace="0" vspace="0">\1</iframe><br><a href="\1" target="_blank">如果你的浏览器不支持嵌入框,请点这里查看</a>&#39;,
                        &#39;<iframe src="\2" name="ifr1" frameborder="0" allowtransparency="true" scrolling="yes" width="100%" height="\1" marginwidth="0" marginheight="0" hspace="0" vspace="0">\2</iframe><br><a href="\2" target="_blank">如果你的浏览器不支持嵌入框,请点这里查看</a>&#39;,
                        ));
                }
                if($this->flash){
                        $reg_ary = array_merge($reg_ary,array(
                        &#39;/\[swf\]\s*(.+?)\s*\[\/swf\]/i&#39;,
                        &#39;/\[swf=(\d+)\,(\d+)\]\s*(.+?)\s*\[\/swf\]/i&#39;
                        ));
                        $rpl_ary = array_merge($rpl_ary,array(
                        &#39;<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"><param name="movie" value="\1" /><param name="quality" value="high" /><embed src="\1" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></object>&#39;,
                        &#39;<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="\1" height="\2"><param name="movie" value="\3" /><param name="quality" value="high" /><embed src="\3" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="\1" height="\2"></embed></object>&#39;
                        ));
                }
         		if($this->others){//处理特殊ubb问题
                        $reg_ary = array_merge($reg_ary,array(
                        &#39;/\[upload=\d+]/i&#39;,
                        &#39;/\[attachment=(\d+?)\]/i&#39;,
                        &#39;/\[attach\]\s*(\d+?)\s*\[\/attach\]/i&#39;,
                        &#39;/\[qq\]\s*(\d+?)\s*\[\/qq\]/i&#39;,
                        &#39;/\[indent\]/i&#39;,
                        &#39;/\[\/indent\]/i&#39;
                        ));
                        $rpl_ary = array_merge($rpl_ary,array(
						&#39;&#39;, 
                        &#39;&#39;,
                        &#39;\\1&#39;,
                        &#39;QQ:\\1&#39;,
                        &#39;<blockquote>&#39;,
                        &#39;</blockquote>&#39;                       
                        ));
                }
                if(sizeof($reg_ary)&&sizeof($rpl_ary))$str = preg_replace($reg_ary,$rpl_ary,$str);
                if($this->php) {
                        $tmp = $str[0];
                        for($i=0; $i<sizeof($ary[4]); $i++) {
                                ob_start();
                                highlight_string(trim(strtr($ary[4][$i],array(&#39;&lt;&#39;=>&#39;<&#39;,&#39;&gt;&#39;=>&#39;>&#39;," "=>" ","<br />"=>""))));
                                $tmp .= &#39;<table border=1 cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#055AA0" width=95%><tr><td><code>&#39;.(trim($ary[3][$i])?trim($ary[3][$i]):&#39;代码片段:&#39;).&#39;</code><br /><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td width="100%" class="code">&#39;.ob_get_contents().&#39;</td></tr></table></td></tr></table>&#39;.$str[$i+1];
                                ob_end_clean();
                        }
                        $str = $tmp;
                        unset($tmp);
                }
                return $str;
        }

}
?>
로그인 후 복사

                   

                   

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

이번 장에서는 CakePHP의 환경 변수, 일반 구성, 데이터베이스 구성, 이메일 구성에 대해 알아봅니다.

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP 데이터베이스 작업 CakePHP 데이터베이스 작업 Sep 10, 2024 pm 05:25 PM

CakePHP에서 데이터베이스 작업은 매우 쉽습니다. 이번 장에서는 CRUD(생성, 읽기, 업데이트, 삭제) 작업을 이해하겠습니다.

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

cakephp4에서 날짜와 시간을 다루기 위해 사용 가능한 FrozenTime 클래스를 활용하겠습니다.

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

파일 업로드 작업을 위해 양식 도우미를 사용할 것입니다. 다음은 파일 업로드의 예입니다.

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

이번 장에서는 라우팅과 관련된 다음과 같은 주제를 학습하겠습니다.

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

CakePHP 유효성 검사기 만들기 CakePHP 유효성 검사기 만들기 Sep 10, 2024 pm 05:26 PM

컨트롤러에 다음 두 줄을 추가하면 유효성 검사기를 만들 수 있습니다.

See all articles