1. <?php
  2. /**
  3.  * UGiA php syntax highlighter
  4.  * show line number and expand or unexpand the code block
  5.  * 
  6.  * @author    legend <legendsky@hotmail.com>
  7.  * @link      http://www.ugia.cn/?p=55
  8.  * @version   1.0
  9.  * @Copyright www.ugia.cn 
  10.  */
  11.  
  12. /**
  13.  * function highlight
  14.  *
  15.  * @param  string   $code      php code or filename
  16.  * @param  boolean  $isReturn  return as string or print it
  17.  * @param  boolean  $isStr     file or string
  18.  *
  19.  * @return string   $return    if $isReturn is true
  20.  */
  21. function highlight($code$isReturn 0$isStr 1)
  22. {        
  23.     if ($isStr)
  24.     {
  25.         if (!preg_match('#^\s*<\s?\?#si'$code))
  26.         {
  27.             $code "<?php BEGIN__PHP_SYNTAX_HIGLIGHTER $code \r\nEND__PHP_SYNTAX_HIGLIGHTER ?>";
  28.             $addedtags true;
  29.         }
  30.         else
  31.         {
  32.             $addedtags false;
  33.             $code preg_replace("/^\s*<\s\?/isU""<?"$code);
  34.             $code preg_replace("/<\/>$/isU"""$code);
  35.         }
  36.     
  37.         $oldlevel error_reporting(0);
  38.         
  39.         if (PHP_VERSION  >= '4.2.0')
  40.         {
  41.             $buffer highlight_string($codetrue);
  42.         }
  43.         else
  44.         {
  45.             @ob_start();
  46.             highlight_string($code);
  47.             $buffer = @ob_get_contents();
  48.             @ob_end_clean();
  49.         }
  50.         
  51.         error_reporting($oldlevel);
  52.     
  53.         if ($addedtags)
  54.         {
  55.             $buffer preg_replace(array(
  56.                 '#(<|&lt;)\?.*php( |&nbsp;)BEGIN__PHP_SYNTAX_HIGLIGHTER #siU',
  57.                 '#END__PHP_SYNTAX_HIGLIGHTER( |&nbsp;)\?(>|&gt;)#siU'
  58.             ), ''$buffer);
  59.         }        
  60.     }
  61.     else
  62.     {
  63.         @ob_start();
  64.         highlight_file($code);
  65.         $buffer = @ob_get_contents();
  66.         @ob_end_clean();
  67.     }
  68.     
  69.     $buffer preg_replace('/&amp;#([0-9]+);/''&#$1;'$buffer);
  70.     $buffer preg_replace("/<\/?code>/isU"""$buffer);
  71.     $buffer preg_replace('/<FONT COLOR="#/i''<span class="php_'$buffer);
  72.     $buffer preg_replace('/<\/FONT>/i''</span>'$buffer);
  73.     
  74.     $blocks explode("</span><span"$buffer);
  75.     
  76.     $blocktag  0;
  77.     $incomment 0;
  78.     $newline   "";
  79.     $return    "";
  80.     $countb    count($blocks);
  81.     
  82.     $return .= "<div class=\"code\">\n<ol class=\"code\">\n";
  83.     
  84.     for ($i 0$i $countb$i ++)
  85.     {       
  86.         $color substr($blocks[$i], 810);
  87.         $block "<span" $blocks[$i] . "</span>";
  88.         $block str_replace("<br />"'</span><br /><span class="'.$color.'">'$block);
  89.         
  90.         $lines explode("<br />"$block);
  91.         $num   count($lines);
  92.         
  93.         for($j 0$j $num$j ++)
  94.         {   
  95.             if ($color == "php_007700" || $color == "php_FF8000")
  96.             {
  97.                 $counts substr_count($lines[$j], "/*");
  98.                 $counte substr_count($lines[$j], "*/");
  99.                 
  100.                 if ($counts $counte && $incomment == 0
  101.                 {
  102.                     $incomment 1;
  103.                 }
  104.                 elseif ($incomment == 1)
  105.                 {
  106.                     $counts 0;
  107.                 }
  108.                 
  109.                 if ($counte && $incomment == 1
  110.                 {
  111.                     $incomment 0;
  112.                 }
  113.                 
  114.                 $counts += substr_count($lines[$j], "{");
  115.                 $counte += substr_count($lines[$j], "}");
  116.                 
  117.                 if ($counts $counte)
  118.                 {
  119.                     $class $blocktag == "startblock" "substartblock";
  120.                 }
  121.                 elseif ($counts $counte)
  122.                 {
  123.                     $class $blocktag == "endblock" "subendblock";
  124.                 }
  125.                 elseif ($counts == $counte && $j $num 1)
  126.                 {
  127.                     $class $blocktag == "outblock" "inblock";
  128.                 }
  129.                 
  130.                 $blocktag += $counts;
  131.                 $blocktag -= $counte;
  132.             }
  133.             else
  134.             {
  135.                 $class $blocktag == "outblock" "inblock";  
  136.             }
  137.             
  138.             $newline .= $lines[$j];
  139.             if ($j $num 1)
  140.             {   
  141.                 $countl substr_count($newline"{");
  142.                 $countr substr_count($newline"}");
  143.                 
  144.                 if ($countl && $countl == $countr && $incomment == 0)
  145.                 {
  146.                     $class $blocktag == "outblock" "inblock";
  147.                 }
  148.                                 
  149.                 $onclick $class == "startblock" || $class == "substartblock" " onclick=\"expand(this)\"" "";
  150.                 $return .= "<li class=\"$class\"$onclick>$newline</li>\n";
  151.                 
  152.                 $newline "";
  153.             }
  154.         }
  155.     }
  156.     
  157.     $return .= "</ol>\n</div>";
  158.     
  159.     if (!$isReturn) {
  160.         echo $return;
  161.     }
  162.     else
  163.     {
  164.         return $return;
  165.     }
  166. }