1. 论坛系统升级为Xenforo,欢迎大家测试!
    排除公告

原创,从vbb的bb_code中挖出来的,php高亮函数

本帖由 小叶2006-05-04 发布。版面名称:源码讨论

  1. 小叶

    小叶 New Member

    注册:
    2005-09-04
    帖子:
    17,941
    赞:
    33
    PHP:
    <?php
        
    function handle_bbcode_php($code){
        if (!
    is_array($codefind1))
            {
                
    $codefind1 = array(
                    
    '<br>',        // <br> to nothing
                    
    '<br />'    // <br /> to nothing
                
    );
                
    $codereplace1 = array(
                    
    '',
                    
    ''
                
    );
                
    $codefind2 = array(
                    
    '&gt;',        // &gt; to >
                    
    '&lt;',        // &lt; to <
                    
    '&quot;',    // &quot; to ",
                    
    '&amp;',    // &amp; to &
                    
    '[',    // [ to [
                    
    ']',    // ] to ]
                
    );
                
    $codereplace2 = array(
                    
    '>',
                    
    '<',
                    
    '"',
                    
    '&',
                    
    '[',
                    
    ']',
                );
            }
            
    // remove htmlspecialchars'd bits and excess spacing
            
    $code trim(str_replace($codefind1$codereplace1$code));
            
    $code str_replace($codefind2$codereplace2$code); // finish replacements
            // do we have an opening <? tag?
            
    if (!preg_match('#<\?#si'$code))
            {
                
    // if not, replace leading newlines and stuff in a <?php tag and a closing tag at the end
                
    $code "<?php BEGIN__VBULLETIN__CODE__SNIPPET $code \r\nEND__VBULLETIN__CODE__SNIPPET ?>";
                
    $addedtags true;
            }
            else
            {
                
    $addedtags false;
            }
            
    // highlight the string
            
    $oldlevel error_reporting(0);
            if (
    PHP_VERSION  >= '4.2.0')
            {
                
    $code highlight_string($codetrue);
            }
            else
            {
                @
    ob_start();
                
    highlight_string($code);
                
    $code = @ob_get_contents();
                @
    ob_end_clean();
            }
            
    error_reporting($oldlevel);
            
    // if we added tags above, now get rid of them from the resulting string
            
    if ($addedtags)
            {
                
    $search = array(
                    
    '#&lt;\?php( |&nbsp;)BEGIN__VBULLETIN__CODE__SNIPPET( |&nbsp;)#siU',
                    
    '#(<(span|font)[^>]*>)&lt;\?(</\\2>(<\\2[^>]>))php( |&nbsp;)BEGIN__VBULLETIN__CODE__SNIPPET( |&nbsp;)#siU',
                    
    '#END__VBULLETIN__CODE__SNIPPET( |&nbsp;)\?(>|&gt;)#siU'
                
    );
                
    $replace = array(
                    
    '',
                    
    '\\4',
                    
    ''
                
    );
                
    $code preg_replace($search$replace$code);
            }
            
    $code preg_replace('/&amp;#([0-9]+);/''&#$1;'$code); // allow unicode entities back through
            
    $code str_replace(array('['']'), array('['']'), $code);
            return 
    $code;
        }

    $code ="<?php $test= str_replace(array('[', ']'), array('[', ']'), $code);?>";

    echo 
    handle_bbcode_php($code);
    ?>
     
  2. 小叶

    小叶 New Member

    注册:
    2005-09-04
    帖子:
    17,941
    赞:
    33
    :belial:
     
  3. wm_chief

    wm_chief New Member

    注册:
    2005-09-05
    帖子:
    17,890
    赞:
    46
    你真能挖
     
  4. sluke

    sluke New Member

    注册:
    2005-09-04
    帖子:
    4,550
    赞:
    13
    ................
     
  5. 小叶

    小叶 New Member

    注册:
    2005-09-04
    帖子:
    17,941
    赞:
    33
    不错少了几个转义字符,
     
  6. Kevin

    Kevin New Member

    注册:
    2005-10-23
    帖子:
    12,728
    赞:
    55
    挖码机器人~