先给应用示例
例1:
<?php
echo '<style type="text/css">
<!--
.phpcode {
background: #E8FFE8;
}
code {
white-space: nowrap;
}
-->
</style>';
echo highlighter::highlightPhpFile('F:/www/phpMyAdmin/server_binlog.php',0);
?>
例2:
<?php
echo highlighter::highlightHtmlFile('F:/www/kusozone/index.htm',1);
?>
类文件源码
<?php
/*************************************************
* 本单元写于 :
* 作者 : 李腾秋
* E-Mail : litqqs@163.com
* 版权所有
*************************************************/
/*************************************************
作用: 高亮代码
调用时不必创建highlighter类实例,只需highlighter::funcName()即可
方法原型列表:
string highlighter::highlightHtmlFile(string $file, bool $line_number=false)
string highlighter::highlightHtml(string $code, bool $line_number=false)
string highlighter::highlightPhpFile(string $file, bool $and_html=true)
string highlighter::highlightPhp(string $code, bool $and_html=true)
*************************************************/
if(! defined(__FILE__))
{
define(__FILE__,1);
class highlighter
{
function highlighter()
{
}
//高亮htm文件
function highlightHtmlFile($file, $line_number=false)
{
$code = file_get_contents($file);
return highlighter::highlightHtml($code, $line_number);
}
//方法 __pv($m) 仅供 方法 highlightHtml 内调用
function __pv($m)
{
$reg='/=s*"([^"]*?)"/i';
$str = preg_replace($reg, '<font color="#3300FF">=</font><font color="#FF33CC">"${1}"</font>', $m[2]);
return '<'.$m[1].'<font color="#FF6600">' . $str . '</font><font color="#0000FF">'.$m[3].'</font>';
}
//方法 __htmlspace($m) 仅供 方法 highlightHtml 内调用
function __htmlspace($m)
{
$result = str_replace("t", ' ', $m[0]);
$result = str_replace(' ', ' ', $result);
return $result;
}
//高亮html代码
function highlightHtml($code, $line_number=false)
{
$code = htmlspecialchars($code);
//$code = str_replace(" "," ",$code); //替换空格替换为
//$code = nl2br($code); //将回车替换为<br>
//htm标签
$code = preg_replace_callback('/<([a-zA-Z0-9]+)(.*?)(/?>)/',array('highlighter','__pv'), $code);
$code = preg_replace('/(<[a-zA-Z0-9]+)/', '<font color="#0000FF">$1</font>', $code);
$code = preg_replace('/(</[a-zA-Z0-9]+>)/', '<font color="#0000FF">$1</font>', $code);
$code = preg_replace('/(/>)/', '<font color="#0000FF">${1}</font>', $code);
//$code = preg_replace('/(</?[a-zA-Z]+ .*?>)/','<font color="#0000FF">${1}</font>', $code);
$code = preg_replace('/<!DOCTYPEs+.+?>/','<font color="#3300FF">${0}</font>',$code);
//注释
$code = str_replace('<!--', '<font color="#666666"><em><!--', $code);
$code = str_replace('-->', '--></em></font>', $code);
//--------------以下是专为visualTpl模板文件而定制的高亮代码--------
//block : begin|end
$code = preg_replace('/(<!--s*)(begin|end)(s+)([a-z_x7f-xfe]+)/i','${1}<font size="" color="#0000FF"><b>${2}</b></font>${3}<b><font color="#FF0000">${4}</font></b>', $code);
$code = preg_replace('/($[a-z0-9_]+)s*=s*(per|on)(([0-9]+),('.*?'),('.*?'))/i','<font color="#009900"><b>${1}</b></font>=<font color="#0000FF">${2}</font>(${3},<font color="#FF9999">${4}</font>,<font color="#FF9999">${5}</font>)', $code);
//vip : vip|endvip
$code = preg_replace('/<font color="#666666"><em><!--s*vip/i', '<span style="display:block;border:1px dashed #696969;padding 3px" >${0}', $code);
$code = preg_replace('/<!--s*endvips*--></em></font>/i', '${0}</span>', $code);
//ssi : #include
$code = preg_replace('/<!--s*#includes+file.+?-->/i','<span style="background-color:#FFFF66; font-weight:bold; font-style:normal;padding:3px">${0}</span>',$code);
//无格式变量
$code = preg_replace('/({$[a-zA-Z0-9_x7f-xfe]+})/','<font style="background-color:#D7FED1;padding:1px" color="#009900">${1}</font>', $code);
//格式变量
$code = preg_replace('/({$[a-zA-Z0-9_x7f-xfe]+;)([a-zA-Z]+)='([^']+?)'}/','<font style="background-color:#D7FED1;padding:1px" color="#009900">${1}<font color="#CC0000">${2}</font>=<font color="#FF33CC">'${3}'</font>}</font>', $code);
//--------------定制代码结束--------------
$code = preg_replace_callback('/>[^<]+?</',array('highlighter','__htmlspace'), $code);
//$code = nl2br($code);
if(! $line_number){
return '<PRE>'.$code.'</PRE>';
}else{
$code = '<pre><ol><li>' . str_replace("n",'</li><li>',$code) . '</li></ol></pre>';
return $code;
}
}
//高亮php代码,支持php与HTMl混合,同时高亮html
function highlightPhpFile($file,$and_html=true)
{
$code = file_get_contents($file);
return highlighter::highlightPhp($code, $and_html);
}
//高亮php代码,支持php与HTMl混合,同时高亮html
function highlightPhp($code,$and_html=true)
{
$code_array = highlighter::divPhpHtml($code);
$count = count($code_array);
$result='';
if($and_html){
$phpcode=array();
for($i=0;$i<$count;$i++){
if('php'==$code_array[$i][0]){
$phpcode['__inner___php___code_'.$i.'_'] ='<span class="phpcode">'. highlight_string($code_array[$i][1],true).'</span>';
$result .= '__inner___php___code_'.$i.'_';
}else{
$result .= $code_array[$i][1];
}
}
$result = highlighter::highlightHtml($result,false);
//重组php与htm代码
$result = str_replace(array_keys($phpcode), $phpcode, $result);
}else{
for($i=0;$i<$count;$i++){
$hc='';
if('php'==$code_array[$i][0]){
$hc ='<span class="phpcode">'. highlight_string($code_array[$i][1],true).'</span>';
}else{
$hc =htmlspecialchars($code_array[$i][1],true);
$hc =str_replace(' ', ' ', $hc);
$hc =str_replace("t", ' ', $hc);
$hc =nl2br($hc);
}
$result .= $hc;
}
}
$result = str_replace('<code>','',$result);
$result = str_replace('</code>','',$result);
return '<code>'. $result.'</code>';
}
//将php与HTMl混合的代码分开,返回数组
function divPhpHtml($code)
{
$result=array();
$pos=1;
$pre=0;
$in_php_code=false;
$len = strlen($code);
//只有一个或两个字符的的时候
if($pos >= $len-1) {
$result[]= $code;
return $result;
}
while($pos < $len)
{
$symbol = $code[$pos];
$pre_symbol = $code[$pos-1];
$pos++;
if("n"==$symbol || "r" == $symbol) continue;
if(!$in_php_code && '?'==$symbol && '<'==$code[$pos-2]){
$in_php_code=true;
//保存前一段代码
if($pos-2 > $pre){
$html_code = trim(substr($code, $pre, $pos-2 - $pre));
$result[]= array('htm',$html_code);
$pre = $pos-2;
}
continue;
}
//双引号字符串
if('"'==$symbol && $in_php_code){
$in_dqm_string=true;
while($in_dqm_string && $pos < $len){
if('"' != $code[$pos]){
$pos++;
continue;
}
$in_dqm_string=false;
if('' == $code[$pos -1]){
$escape_num=1;
while('' == $code[$pos - $escape_num]) $escape_num++;
$in_dqm_string = 0 == ($escape_num % 2);
}
$pos++;
}
continue;
}
//单引号字符串
if('''==$symbol && $in_php_code){
$in_sqm_string=true;
while($in_sqm_string && $pos < $len){
if(''' != $code[$pos]){
$pos++;
continue;
}
$in_sqm_string=false;
if('' == $code[$pos -1]){
$escape_num=1;
while('' == $code[$pos - $escape_num]) $escape_num++;
$in_sqm_string = 0 == ($escape_num % 2);
}
$pos++;
}
continue;
}
//行注释
if(('/'==$symbol && '/'==$pre_symbol || '#'== $symbol) && $in_php_code){
while("n" != $code[$pos] && "r"!=$code[$pos] && $pos < $len) $pos++;
continue;
}
//块注释
if('*'==$symbol && '/'==$pre_symbol && $in_php_code){
$in_block_comment=true;
while($in_block_comment && $pos < $len){
$in_block_comment = !('/' == $code[$pos] && '*'==$code[$pos-1]);
$pos++;
}
continue;
}
//HereDoc代码块
if('<'==$symbol && '<'==$pre_symbol && '<'==$code[$pos-3] && $in_php_code){
$in_here_doc=true;
while(' '==$code[$pos] || "t"==$code[$pos]) $pos++;
$here_doc_syntax='';
//取here_doc的标识字串
while("n"!=$code[$pos] && "r"!=$code[$pos] && $pos<$len){
$here_doc_syntax .= $code[$pos];
$pos++;
}
$here_doc_syntax .= ';';
while($in_here_doc && $pos<$len){
$pos++;
$line_string='';
while("n"!=$code[$pos] && "r"!=$code[$pos] && $pos<$len){
$line_string .= $code[$pos];
$pos++;
}
$in_here_doc = $line_string != $here_doc_syntax;
}
continue;
}
//php代码块结束
if('>'==$symbol && '?'==$pre_symbol && $in_php_code){
$in_php_code=false;
$php_code = trim(substr($code, $pre, $pos - $pre));
$result[]= array('php',$php_code);
$pre = $pos;
$pos++;
}
}
if($pre != $pos){
$type = ($in_php_code)?'php':'htm';
$result[] = array($type, trim(substr($code, $pre, $pos - $pre)));
}
return $result;
}
}//end class
}//end if
?>
[ 本帖最后由 litqqs 于 2006-7-26 05:35 PM 编辑 ]