推薦Discuz!5的PHP代碼高亮顯示與實現可運行代碼_PHP教程

來源:互聯網
上載者:User
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']] = "

$code
提示:您可以先修改部分代碼再運行
";
$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代碼]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);

http://www.bkjia.com/PHPjc/318001.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/318001.htmlTechArticlediscuz!5.0論壇顯示風格的php代碼高亮顯示外掛程式, 把php代碼進行文法著色,喜歡PHP的同志可以輕鬆的看代碼啦。 同時添加了html代碼運行功能。 此...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.