litqqs
注册会员

初级会员
UID 64079
精华
1
积分 154
帖子 116
金钱 144 喜悦币
威望 10
人脉 0
阅读权限 20
注册 2005-5-12
状态 离线
|
[推荐阅读] 系统严重安全警告
如你所愿,欢迎测试.
就花几个钟,为大众贡献一点代码.如有出错的地方,还请大家提出来,以便修正.
测试的次数不是太多.难免会有出错的地方.请大家海涵
测试环境:
php4/php5
windowXP
本人是以命令行方式执行.
<?php $html_php_code = file_get_contents('e:\ttt.php'); print_r(divPhpHtml($html_php_code));
//功能函数 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 = substr($code, $pre, $pos-2 - $pre); $result[]= $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 = 1 == ($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_dqm_string = 1 == ($escape_num % 2); } } $pos++; continue; } //行注释 if('/'==$symbol && '/'==$pre_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 .= ';'; echo $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 = substr($code, $pre, $pos - $pre); $result[]= $php_code ; $pre = $pos; $pos++; } } if($pre != $pos){ $result[] = substr($code, $pre, $pos - $pre); } return $result; } ?> [ 本帖最后由 litqqs 于 2006-6-20 05:48 PM 编辑 ]
|
|