/**
* 代码加亮
* @param String $str 要高亮显示的字符串 或者 文件名
* @param Boolean $show 是否输出
* @return String
*/
function highlight_code($str,$show=false) {
if(file_exists($str)) {
$str = file_get_contents($str);
}
$str = stripslashes(trim($str));
$str = str_replace(array('<', '>'), array('<', '>'), $str);
$str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str);
$str = '<?php //tempstart'."\n".$str.'//tempend ?>'; // <?
$str = highlight_string($str, TRUE);
if (abs(phpversion()) < 5) {
$str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
$str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
}
$str = preg_replace("#\<code\>.+?//tempstart\<br />\</span\>#is", "<code>\n", $str);
$str = preg_replace("#\<code\>.+?//tempstart\<br />#is", "<code>\n", $str);
$str = preg_replace("#//tempend.+#is", "</span>\n</code>", $str);
$str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //<?
$line = explode("<br />", rtrim(ltrim($str,'<code>'),'</code>'));
$result = '<div class="code"><ol>';
foreach($line as $key=>$val) {
$result .= '<li>'.$val.'</li>';
}
$result .= '</ol></div>';
$result = str_replace("\n", "", $result);
if( $show!== false) {
echo($result);
}else {
return $result;
}
}