插件演示地址:Discuz!论坛实现PHP代码高亮:http://www.phpq.net/bbs/thread-5-1-1.html
插件添加步骤如下:
修改include/discuzcode.func.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]";
- }
复制代码 在下面添加:- 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
查找- if($parsetype != 1 && !$bbcodeoff && $allowbbcode) {
- $message = preg_replace("/\s*\[code\](.+?)\[\/code\]\s*/ies", "codedisp('\\1')", $message);
- }
复制代码 在其下面添加:-
- if($parsetype != 1 && !$bbcodeoff && $allowbbcode) {
- $message = preg_replace("/\s*\[php\](.+?)\[\/php\]\s*/ies", "phpcodedisp('\\1')", $message);
- }
复制代码 修改include/javascript/editor.js
查找- insertText(text, strlen(opentag), strlen(closetag), in_array(tagname, ['code', 'quote', 'free', 'hide']) ? true : false);
复制代码 修改为- insertText(text, strlen(opentag), strlen(closetag), in_array(tagname, ['code', 'quote', 'free', 'hide', 'php']) ? true : false);
复制代码 继续修改include/javascript/editor.js
查找- if(in_array(cmd, ['quote', 'code', 'free', 'hide'])) {
复制代码 修改为- if(in_array(cmd, ['quote', 'code', 'free', 'hide', 'php'])) {
复制代码 6.修改templates/default/post_bbinsert.htm
查找- <td><a id="{$editorid}_cmd_code"><img src="images/common/bb_code.gif" title="{lang post_discuzcode_code}" alt="Code" /></a></td>
复制代码 在下面添加- <td><a id="{$editorid}_cmd_php"><img src="images/common/bb_php.gif" title="HP代码" alt="php" /></a></td>
复制代码 然后上传bb_php.gif到 images/common 目录下 |