Discuz!5的PHP代码高亮显示插件(黑暗中的舞者更新)


Posted in PHP onJanuary 29, 2007

discuz!5.0论坛显示风格的php代码高亮显示插件,
把php代码进行语法着色,喜欢PHP的同志可以轻松的看代码啦。
同时添加了html代码运行功能。
此插件是在以前某位同志发布的Discuz!4的php代码高亮显示基础之上修改完成。
另外,本人的php学习进入了停滞状态,
希望有人能帮助我走出php的学习困境
My QQ:5642382  我的QQ群:2577162

功能:发贴时使用[php]php代码[/php]标记来高亮显示php代码,
      使用[runcode]Html代码[/runcode]标记来运行HTML代码(在客户端运行非服务器)。

插件添加步骤如下:
1.修改include/common.js
  查找
function copycode(obj) {
        var rng = document.body.createTextRange();
        rng.moveToElementText(obj);
        rng.scrollIntoView();
        rng.select();
        rng.execCommand("Copy");
        rng.collapse(false);
}
在这个函数结束的下面添加:

[Copy to clipboard] [ - ]CODE:
function runCode(obj) { 
        var winname = window.open('', "_blank", ''); 
        winname.document.open('text/html', 'replace'); 
        winname.document.writeln(obj.value); 
        winname.document.close(); 

function saveCode(obj) { 
        var winname = window.open('', '_blank', 'top=10000'); 
        winname.document.open('text/html', 'replace'); 
        winname.document.writeln(obj.value); 
        winname.document.execCommand('saveas','','code.htm'); 
        winname.close(); 

2.修改include/discuzcode.func.php
  查找

[Copy to clipboard] [ - ]CODE:
        $discuzcodes['codecount']++;
        return "[\tDISCUZ_CODE_$discuzcodes[pcodecount]\t]";
}
(注:也可以用Editplus直接查找到代码的第110行来进行下面的添加)
在下面继续添加:

/------- 添加 [runcode] Html代码 [/runcode] 来运行Html代码
function runcodedisp($code) { 
        global $discuzcodes; 
        $discuzcodes['pcodecount']++; 
        $code = htmlspecialchars(str_replace('\\"', '"', preg_replace("/^[\n\r]*(.+?)[\n\r]*$/is", "\\1", $code))); 
        $discuzcodes['codehtml'][$discuzcodes['pcodecount']] = "<br><br><div class=\"smalltxt\" style=\"margin-left: 2em; margin-right: 2em\"><textarea name=\"runcode$discuzcodes[codecount]\" rows=\"1\" cols=\"95\" style=\"height:200px\">$code</textarea><br><input type=\"button\" value=\"运行代码\" onclick=\"runCode(runcode$discuzcodes[codecount])\"> <input type=\"button\" value=\"复制代码\" onclick=\"copycode(runcode$discuzcodes[codecount])\"> <input type=\"button\" value=\"另存代码\" onclick=\"saveCode(runcode$discuzcodes[codecount])\"> <script language=\"JavaScript\">ffcod = delpost.runcode$discuzcodes[codecount] .value; ffcod = ffcod.replace(/<br \/>/g,''); delpost.runcode$discuzcodes[codecount] .value = ffcod;</script> 提示:您可以先修改部分代码再运行</div><br>"; 
        $discuzcodes['codecount']++; 
        return "[\tDISCUZ_CODE_$discuzcodes[pcodecount]\t]"; 

//------- 添加  [php] php代码 [/php] 来高亮显示php代码,进行php语法着色
function phpcodedisp($code) { 
        global $discuzcodes; 
        $discuzcodes['pcodecount']++; 
        $code = phphighlite(str_replace("\\\"", "\"", $code)); 
        $discuzcodes['codehtml'][$discuzcodes['pcodecount']] = "<br><div class=\"msgheader\"><div class=\"right\"><a href=\"###\" class=\"smalltxt\" onclick=\"copycode($('phpcode$discuzcodes[codecount]'));\">[复制PHP代码]</a></div>PHP代码如下:</div><div class=\"msgborder\" id=\"phpcode$discuzcodes[codecount]\">$code</div><br>"; 
        $discuzcodes['codecount']++; 
        return "[\tDISCUZ_CODE_$discuzcodes[pcodecount]\t]"; 

function phphighlite($code) { 
        if(!strpos($code,"<?\n") && !strpos($code,'<?') && substr($code,0,2) != '<?') { 
                $code = '<'.'?'.trim($code).' ?'.'>'; 
                $addedtags = 1; 
        } 
        ob_start(); 
        $oldlevel = error_reporting(0); 
        highlight_string($code); 
        error_reporting($oldlevel); 
        $buffer = ob_get_contents(); 
        ob_end_clean(); 
        if ($addedtags) { 
                $openingpos = strpos($buffer, '<?'); 
                $closingpos = strrpos($buffer, '?'); 
                $buffer = substr($buffer, 0, $openingpos).substr($buffer, $openingpos+5, $closingpos-($openingpos+5)).substr($buffer, $closingpos+5); 
        } 
        $buffer = str_replace('"', "\"", $buffer); 
        $buffer = str_replace('<br />', '', $buffer); 
        return $buffer; 

//------- 结束
3.继续修改include/discuzcode.func.php
  查找

$message = preg_replace("/\s*\[code\](.+?)\[\/code\]\s*/ies", "codedisp('\\1')", $message);
在其下面继续添加:

[Copy to clipboard] [ - ]CODE:
//------- runcode 
$message = preg_replace("/\s*\[runcode\](.+?)\[\/runcode\]\s*/ies", "runcodedisp('\\1')", $message); 
//------- php 
$message = preg_replace("/\s*\[php\](.+?)\[\/php\]\s*/ies", "phpcodedisp('\\1')", $message);
[ 本帖最后由 yzxicq0 于 2006-9-4 17:11 编辑 ]

PHP 相关文章推荐
php提示Warning:mysql_fetch_array() expects的解决方法
Dec 16 PHP
php计算指定目录下文件占用空间的方法
Mar 13 PHP
帝国CMS留言板回复后发送EMAIL通知客户
Jul 06 PHP
PHP扩展开发教程(总结)
Nov 04 PHP
标准版Eclipse搭建PHP环境的详细步骤
Nov 18 PHP
Symfony2使用Doctrine进行数据库查询方法实例总结
Mar 18 PHP
php使用str_replace替换多维数组的实现方法分析
Jun 15 PHP
PHP中的empty、isset、isnull的区别与使用实例
Mar 22 PHP
浅析PHP7 的垃圾回收机制
Sep 06 PHP
laravel-admin 中列表筛选方法
Oct 03 PHP
laravel框架 laravel-admin上传图片到oss的方法
Oct 13 PHP
TP5框架实现自定义分页样式的方法示例
Apr 05 PHP
解决控件遮挡问题:关于有窗口元素和无窗口元素
Jan 28 #PHP
获得Google PR值的PHP代码
Jan 28 #PHP
一键删除顽固的空文件夹 软件下载
Jan 26 #PHP
php中通过smtp发邮件的类,测试通过
Jan 22 #PHP
php5.2.0内存管理改进
Jan 22 #PHP
php中截取字符串支持utf-8
Jan 18 #PHP
php中的登陆login
Jan 18 #PHP
You might like
修改了一个很不错的php验证码(支持中文)
2007/02/14 PHP
DISCUZ 论坛管理员密码忘记的解决方法
2009/05/14 PHP
Ajax+PHP 边学边练之四 表单
2009/11/27 PHP
JQuery AJAX 中文乱码问题解决
2013/06/05 Javascript
js 控制页面跳转的5种方法
2013/09/09 Javascript
Javascript设置对象的ReadOnly属性(示例代码)
2013/12/25 Javascript
js判断url是否有效的两种方法
2014/03/04 Javascript
在Firefox下js select标签点击无法弹出
2014/03/06 Javascript
jquery背景跟随鼠标滑动导航
2015/11/20 Javascript
JS函数arguments数组获得实际传参数个数的实现方法
2016/05/28 Javascript
JS点击某个图标或按钮弹出文件选择框的实现代码
2016/09/27 Javascript
JS日程管理插件FullCalendar简单实例
2017/02/07 Javascript
js, jQuery实现全选、反选功能
2017/03/08 Javascript
微信小程序本地缓存数据增删改查实例详解
2017/05/24 Javascript
vue实现单选和多选功能
2017/08/11 Javascript
使用JS组件实现带ToolTip验证框的实例代码
2017/08/23 Javascript
如何为vue的项目添加单元测试
2018/12/19 Javascript
分享一个可以生成各种进制格式IP的小工具实例代码
2017/07/28 Python
Pandas实现数据类型转换的一些小技巧汇总
2018/05/07 Python
Flask框架Flask-Principal基本用法实例分析
2018/07/23 Python
Python3 安装PyQt5及exe打包图文教程
2019/01/08 Python
python简单区块链模拟详解
2019/07/03 Python
Python爬取破解无线网络wifi密码过程解析
2019/09/17 Python
如何基于Python实现数字类型转换
2020/02/07 Python
Python中os模块功能与用法详解
2020/02/26 Python
Python Django view 两种return的实现方式
2020/03/16 Python
使用python把xmind转换成excel测试用例的实现代码
2020/10/12 Python
分享29个基于Bootstrap的HTML5响应式网页设计模板
2015/11/19 HTML / CSS
澳大利亚领先的时尚内衣零售商:Bras N Things
2020/07/28 全球购物
大学生翘课检讨书范文
2014/10/06 职场文书
个人批评与自我批评材料
2014/10/17 职场文书
2016年毕业实习心得体会范文
2015/10/09 职场文书
2019年最新感恩节祝福语(28句)
2019/11/27 职场文书
MySQL 分页查询的优化技巧
2021/05/12 MySQL
python微信智能AI机器人实现多种支付方式
2022/04/12 Python
golang用type-switch判断interface的实际存储类型
2022/04/14 Golang