插件演示地址:Discuz!论坛实现PHP代码高亮:http://www.phpq.net/bbs/thread-5-1-1.html
插件添加步骤如下:
修改include/discuzcode.func.php
查找
[php]function codedisp($code) {
global $discuzcodes;
$discuzcodes['pcodecount']++;
$code = htmlspecialchars(str_replace('\\"', '"', preg_replace("/^[\n\r]*(.+?)[\n\r]*$/is", "\\1", $code)));
$discuzcodes['codehtml'][$discuzcodes['pcodecount']] = tpl_codedisp($discuzcodes, $code);
$discuzcodes['codecount']++;
return "[\tDISCUZ_CODE_$discuzcodes[pcodecount]\t]";
}[/php]
在下面添加:
CODE:
function phpcodedisp($code) {
global $discuzcodes;
$discuzcodes['pcodecount']++;
$code = phphighlite(str_replace('\\"', '"', preg_replace("/^[\n\r]*(.+?)[\n\r]*$/is", "\\1", $code)));
$discuzcodes['codehtml'][$discuzcodes['pcodecount']] = "<div class=\"blockcode\"><span class=\"headactions\" onclick=\"copycode($('code{$discuzcodes['codecount']}'));\">复制内容到剪贴板</span><h5>HP代码:</h5><code id=\"code{$discuzcodes['codecount']}\">{$code}</code></div>";
$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, $openingpos+1, $closingpos-($openingpos+1)).substr($buffer, $closingpos+5);
}
$buffer = str_replace('"', "\"", $buffer);
$buffer = str_replace('
', '', $buffer);
return $buffer;
}
继续修改include/discuzcode.func.php
查找[php]if($parsetype != 1 && !$bbcodeoff && $allowbbcode) {
$message = preg_replace("/\s*\[code\](.+?)\[\/code\]\s*/ies", "codedisp('\\1')", $message);
}[/php]在其下面添加:[php]
if($parsetype != 1 && !$bbcodeoff && $allowbbcode) {
$message = preg_replace("/\s*\[php\](.+?)\[\/php\]\s*/ies", "phpcodedisp('\\1')", $message);
}[/php]修改include/javascript/editor.js
查找
CODE:
insertText(text, strlen(opentag), strlen(closetag), in_array(tagname, ['code', 'quote', 'free', 'hide']) ? true : false);
修改为
CODE:
insertText(text, strlen(opentag), strlen(closetag), in_array(tagname, ['code', 'quote', 'free', 'hide', 'php']) ? true : false);
继续修改include/javascript/editor.js
查找
CODE:
if(in_array(cmd, ['quote', 'code', 'free', 'hide'])) {
修改为
CODE:
if(in_array(cmd, ['quote', 'code', 'free', 'hide', 'php'])) {
6.修改templates/default/post_bbinsert.htm
查找
CODE:
<td><a id="{$editorid}_cmd_code"><img src="images/common/bb_code.gif" title="{lang post_discuzcode_code}" alt="Code" /></a></td>
在下面添加
CODE:
<td><a id="{$editorid}_cmd_php"><img src="images/common/bb_php.gif" title="HP代码" alt="php" /></a></td>
然后上传bb_php.gif到 images/common 目录下