Home >Backend Development >PHP Tutorial >Recommended Discuz!5 PHP code highlighting and implementation of runnable code_PHP tutorial

Recommended Discuz!5 PHP code highlighting and implementation of runnable code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:56:54980browse

discuz! 5.0 forum display style PHP code highlighting plug-in,
syntax coloring PHP code, comrades who like PHP can easily read the code.
At the same time, the html code running function has been added.
This plug-in is modified based on the php code highlighting of Discuz!4 released by someone before.
In addition, my PHP learning has entered a stagnant state.
I hope someone can help me get out of the PHP learning dilemma
My QQ:5642382 My QQ group: 2577162

Function: Posting Use the [php]php code[/php] tag to highlight php code, and use the [runcode]Html code[/runcode] tag to run HTML code (running on the client, not the server).

The steps to add the plug-in are as follows:
1. Modify include/common.js
Find

Copy the code The code is as follows:
function copycode(obj) {
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
}

Add below the end of this function:

Copy code The code is as follows:
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. Modify include/discuzcode.func.php
Find

Copy code The code is as follows:
           $discuzcodes['codecount']++;                                                                                                                                          >

(Note: You can also use Editplus to directly find the 110th line of the code to add the following)
Continue to add below:


Copy code
The code is as follows:

//------- 添加 [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']] = "


    提示:您可以先修改部分代码再运行

"; 
        $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']] = "
PHP代码如下:
$code

"; 
        $discuzcodes['codecount']++; 
        return "[tDISCUZ_CODE_$discuzcodes[pcodecount]t]"; 


function phphighlite($code) { 
        if(!strpos($code,"                $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('', '', $buffer); 
        return $buffer; 

//------- 结束

3.继续修改include/discuzcode.func.php
  查找
复制代码 代码如下:

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

在其下面继续添加:
复制代码 代码如下:

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/318001.htmlTechArticlediscuz!5.0论坛显示风格的php代码高亮显示插件, 把php代码进行语法着色,喜欢PHP的同志可以轻松的看代码啦。 同时添加了html代码运行功能。 此...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn