-
<?php
- /**
- * UGiA php syntax highlighter
- * show line number and expand or unexpand the code block
- *
- * @author legend <legendsky@hotmail.com>
- * @link http://www.ugia.cn/?p=55
- * @version 1.0
- * @Copyright www.ugia.cn
- */
-
- /**
- * function highlight
- *
- * @param string $code php code or filename
- * @param boolean $isReturn return as string or print it
- * @param boolean $isStr file or string
- *
- * @return string $return if $isReturn is true
- */
- function highlight($code, $isReturn = 0, $isStr = 1)
- {
- if ($isStr)
- {
- if (!preg_match('#^\s*<\s?\?#si', $code))
- {
- $code = "<?php BEGIN__PHP_SYNTAX_HIGLIGHTER $code \r\nEND__PHP_SYNTAX_HIGLIGHTER ?>";
- $addedtags = true;
- }
- else
- {
- $addedtags = false;
- $code = preg_replace("/^\s*<\s\?/isU", "<?", $code);
- $code = preg_replace("/<\/>$/isU", "", $code);
- }
-
- $oldlevel = error_reporting(0);
-
- if (PHP_VERSION >= '4.2.0')
- {
- $buffer = highlight_string($code, true);
- }
- else
- {
- @ob_start();
- highlight_string($code);
- $buffer = @ob_get_contents();
- @ob_end_clean();
- }
-
- error_reporting($oldlevel);
-
- if ($addedtags)
- {
- $buffer = preg_replace(array(
- '#(<|<)\?.*php( | )BEGIN__PHP_SYNTAX_HIGLIGHTER #siU',
- '#END__PHP_SYNTAX_HIGLIGHTER( | )\?(>|>)#siU'
- ), '', $buffer);
- }
- }
- else
- {
- @ob_start();
- highlight_file($code);
- $buffer = @ob_get_contents();
- @ob_end_clean();
- }
-
- $buffer = preg_replace('/&#([0-9]+);/', '&#$1;', $buffer);
- $buffer = preg_replace("/<\/?code>/isU", "", $buffer);
- $buffer = preg_replace('/<FONT COLOR="#/i', '<span class="php_', $buffer);
- $buffer = preg_replace('/<\/FONT>/i', '</span>', $buffer);
-
- $blocks = explode("</span><span", $buffer);
-
- $blocktag = 0;
- $incomment = 0;
- $newline = "";
- $return = "";
- $countb = count($blocks);
-
- $return .= "<div class=\"code\">\n<ol class=\"code\">\n";
-
- for ($i = 0; $i < $countb; $i ++)
- {
- $color = substr($blocks[$i], 8, 10);
- $block = "<span" . $blocks[$i] . "</span>";
- $block = str_replace("<br />", '</span><br /><span class="'.$color.'">', $block);
-
- $lines = explode("<br />", $block);
- $num = count($lines);
-
- for($j = 0; $j < $num; $j ++)
- {
- if ($color == "php_007700" || $color == "php_FF8000")
- {
- $counts = substr_count($lines[$j], "/*");
- $counte = substr_count($lines[$j], "*/");
-
- if ($counts - $counte > 0 && $incomment == 0)
- {
- $incomment = 1;
- }
- elseif ($incomment == 1)
- {
- $counts = 0;
- }
-
- if ($counte > 0 && $incomment == 1)
- {
- $incomment = 0;
- }
-
- $counts += substr_count($lines[$j], "{");
- $counte += substr_count($lines[$j], "}");
-
- if ($counts > $counte)
- {
- $class = $blocktag == 0 ? "startblock" : "substartblock";
- }
- elseif ($counts < $counte)
- {
- $class = $blocktag == 1 ? "endblock" : "subendblock";
- }
- elseif ($counts == $counte && $j < $num - 1)
- {
- $class = $blocktag == 0 ? "outblock" : "inblock";
- }
-
- $blocktag += $counts;
- $blocktag -= $counte;
- }
- else
- {
- $class = $blocktag == 0 ? "outblock" : "inblock";
- }
-
- $newline .= $lines[$j];
- if ($j < $num - 1)
- {
- $countl = substr_count($newline, "{");
- $countr = substr_count($newline, "}");
-
- if ($countl > 0 && $countl == $countr && $incomment == 0)
- {
- $class = $blocktag == 0 ? "outblock" : "inblock";
- }
-
- $onclick = $class == "startblock" || $class == "substartblock" ? " onclick=\"expand(this)\"" : "";
- $return .= "<li class=\"$class\"$onclick>$newline</li>\n";
-
- $newline = "";
- }
- }
- }
-
- $return .= "</ol>\n</div>";
-
- if (!$isReturn) {
- echo $return;
- }
- else
- {
- return $return;
- }
- }