Home  >  Article  >  Backend Development  >  discuz 7.0 recommends Discuz!5's PHP code highlighting and implementation of runnable code

discuz 7.0 recommends Discuz!5's PHP code highlighting and implementation of runnable code

WBOY
WBOYOriginal
2016-07-29 08:36:34662browse

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);
}


在这个函数结束的下面添加:

复制代码 代码如下:


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
  查找

复制代码 代码如下:


        $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']] = "


/g,''); delpost.runcode$discuzcodes[codecount] .value = ffcod; 提示:您可以先修改部分代码再运行
& lt; br & gt; ";
$ discuzcodes ['codecount'] ++;
Return" [tdiscuz_code_ $ discuzcodes [pcodecount] t] "
// --------------- Add [php] php code [/php] Come on highlight the PHP code, perform PHP syntax color
Function PHPCODISP ($ code) {
Global $ discuzcodes;
$ discuzcodes ['pcodecount'] ++; hphighLite (str_replace ("\" ", """, $code));
$discuzcodes['codehtml'][$discuzcodes['pcodecount']] = "

"; code) {
       if(!strpos($code,"             $code = '<'.'?'.trim($code).' ?'.'>';                                                                                           ;
          $oldlevel = error_reporting(0) ;
          Highlight_string($code);                                                   use using {
                                                                                                                                                                                                                        Out               Out       Out     Out ’ Out ’ Out ’ Out Out Out           off off off 到到到账速度到;
             $closingpos = strrpos($buffer, '?'); .substr($buffer, $closingpos+5);
                                                                                                                                                                      substr (


$message = preg_replace("/s*[code](.+?)[/code]s*/ies", "codedisp('\1')", $message);


Continue below Add:



Copy code

The code is as follows:


//------- runcode
$message = preg_replace("/s*[runcode](.+?)[/runcode]s* /ies", "runcodedisp('\1')", $message);
//------- php
$message = preg_replace("/s*[php](.+?)[/php ]s*/ies", "phpcodedisp('\1')", $message);

The above introduces the PHP code highlighting and implementation of runnable code in discuz 7.0 and recommends Discuz!5, including the content of discuz 7.0. I hope it will be helpful to friends who are interested in PHP tutorials.