Discuz!论坛实现PHP代码高亮

时间:2010-01-09 08:29:14   来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:ction codedisp($code) {        global $discuzcodes;        $discuzcodes['pcodecount']++;        $code = htmlspecialchars(str_replace('//"', '"', preg_replace("/^[/n/r]*(.+?)[/n/r]*$/is", "//1", $code)));      ...
  正文:

插件演示地址: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);
来源:http://www.002pc.com/master/College/Programming/PHP/11656.html

收藏到:

关于《Discuz!论坛实现PHP代码高亮》文章的评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面

随机文章

    SQL Error: select * from ***_ecms_article where classid='86' and checked=1 order by rand() limit 10

顶出来的热门

    SQL Error: select * from ***_ecms_article where classid='86' and checked=1 order by diggtop desc,id desc limit 10
站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td