function getFileContent($filePath, $funcColor) { $type = "error"; $fileExtenstion = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); $tempArray = array("错误, 不能打开该类型文件(如有疑问,请联系张培修复!)"); // *** 类型请用小写字母 *** $textPostfixList = array("txt", "java", "properties", "property", "json", "xml", "sh", "bat", "cmd", "sql", "py", "pl", "cpp", "c", "rb", "php", "html", "js", "jsp", "css", "template", "online", "mf"); $imgPostfixList = array("gif", "jpg", "bmp", "png", "ico"); $linkPostfixList = array("docx", "doc", "pdf", "ppt", "pptx"); $funcNameList = array(); if (in_array($fileExtenstion, $textPostfixList)) { $tempArray = array(); $lines = file($filePath); foreach ($lines as $line) { $line = htmlspecialchars($line); $arr = array($line, ""); if ($fileExtenstion == "java") { $arr = getFormatLine($line, $funcNameList, $funcColor); } array_push($tempArray, $arr); } $type = "text"; } elseif (in_array($fileExtenstion, $imgPostfixList)) { $type = "image"; $filePath = str_replace("/disk2/qatest/svn_code/qa/WebFramework/htdocs", "", $filePath); $tempArray = array("<img src='{$filePath}'/>"); } elseif (in_array($fileExtenstion, $linkPostfixList)) { $type = "image"; $filePath = str_replace("/disk2/qatest/svn_code/qa/WebFramework/htdocs", "", $filePath); $tempArray = array("<a href='{$filePath}' target=_blank>下载</a>"); } return array("type" => $type, "lines" => $tempArray, "funcNames" => $funcNameList); }
function getFileContent($filePath) { $funcNameList = array(); function getFormatLine($line, $linesCnt, $index, &$funcNameList) { ### notice: found global $funcNameList can't work $lineNoColor = "blue"; $funcLine = false; $funcName = ""; // 因为返回类型形如List<Click>中的<,>被转义了,所以正则中;&被捕获 // .主要用来处理全名称的对象类型 // ,主要用于map类型 // ?主要用于List<? > // []主要用于类似Invoice[]返回类型 if (preg_match("/(public|private|protected)\\s+([\\w\\.\\s;&,\\?\\[\\]]+?)\\s+(\\w+)\\s*\\(/", $line, $matches)) { $funcName = $matches[3]; if ($funcName != "main") { array_push($funcNameList, $funcName); $line = preg_replace("/(public|private|protected)\\s+([\\w\\.\\s;&,\\?\\[\\]]+?)\\s+(\\w+)\\s*\\(/", "\$1 \$2 <font color='red'><b>\$3 </b></font>(", $line); $funcLine = true; } } if ($linesCnt < 10) { $line = sprintf("<font color='{$lineNoColor}'>%s </font> %s", $index, $line); } elseif ($linesCnt < 100) { $line = sprintf("<font color='{$lineNoColor}'>%2s </font> %s", $index, $line); } elseif ($linesCnt < 1000) { $line = sprintf("<font color='{$lineNoColor}'>%3s </font> %s", $index, $line); } elseif ($linesCnt < 10000) { $line = sprintf("<font color='{$lineNoColor}'>%4s </font> %s", $index, $line); } if ($funcLine == true) { return sprintf("<span id='func_name_%s'>%s</span>", $funcName, $line); } else { return "<span style='font-size: 1.2em'>{$line}</span>"; } } $fileExtenstion = strtolower(substr(strrchr($filePath, '.'), 1)); $fileContent = "不能打开该类型文件(如有疑问,请联系张培修复!)"; $textPostfixList = array("txt", "java", "properties", "property", "json", "xml", "sh", "bat", "cmd", "sql", "py", "pl", "cpp", "c", "rb", "php", "html", "js", "jsp", "css", "template"); $imgPostfixList = array("gif", "jpg", "bmp", "png", "ico"); if (in_array($fileExtenstion, $textPostfixList)) { $lines = file($filePath); $linesCnt = count($lines); $tempArray = array(); $index = 1; foreach ($lines as $line) { $line = str_replace("&", "&", $line); $line = str_replace("<", "<", $line); $line = str_replace(">", ">", $line); $line = getFormatLine($line, $linesCnt, $index, $funcNameList); array_push($tempArray, $line); $index += 1; } $fileContent = implode("", $tempArray); } elseif (in_array($fileExtenstion, $imgPostfixList)) { $fileContent = "<img src='{$filePath}'/>"; } $funcNameList2 = array_count_values($funcNameList); $funcNameList3 = array(); foreach ($funcNameList2 as $key => $val) { if ($val != 1) { array_push($funcNameList3, "<option name='{$key}'>{$key}({$val})</option>"); } else { array_push($funcNameList3, "<option name='{$key}'>{$key}</option>"); } } return array("<pre>\n{$fileContent}\n</pre>\n", implode("\n", $funcNameList3)); }