php查找文件内容关键字实例代码
Release: 2016-06-13 10:38:49
Original
2080 people have browsed it
- /**
- * 文件: search.php
- * 功能: 搜索指定目录下的HTML文件
- */
- /* 基本函数 */
-
- //获取目录下文件函数
- function getFile($dir)
- {
- $dp = opendir($dir);
- $fileArr = array();
- while (!false == $curFile = readdir($dp)) {
- if ($curFile!="." && $curFile!=".." && $curFile!="") {
- if (is_dir($curFile)) {
- $fileArr = getFile($dir."/".$curFile);
- } else {
- $fileArr[] = $dir."/".$curFile;
- }
- }
- }
- return $fileArr;
- }
-
- //获取文件内容
- function getFileContent($file)
- {
- if (!$fp = fopen($file, "r")) {
- die("Cannot open file $file");
- }
- while ($text = fread($fp, 4096)) {
- $fileContent .= $text;
- }
- return $fileContent;
- }
-
- //搜索指定文件
- function searchText($file, $keyword)
- {
- $text = getFileContent($file);
- if (preg_match("/$keyword/i", $text)) {
- return true;
- }
- return false;
- }
-
- //搜索出文章的标题
- function getFileTitle($file, $default="None subject")
- {
- $fileContent = getFileContent($file);
- $sResult = preg_match("/.*/i", $fileContent, $matchResult);
- $title = preg_replace(array("/()/i","/()/i"), "", $matchResult[0]);
- if (empty($title)) {
- return $default;
- } else {
- return $title;
- }
- }
-
- //获取文件描述信息
- function getFileDescribe($file,$length=200, $default="None describe")
- {
- $metas = get_meta_tags($file);
- if ($meta[description] != "") {
- return $metas[description];
- }
- $fileContent = getFileContent($file);
- preg_match("/()/is", $fileContent, $matchResult);
- $pattern = array("/()/i","/() /i", "/() /i", "/() /i", "/([]) .*([]) /i","/&/i","/"/i","/'/i", "/s/");
- $description = preg_replace($pattern, "", $matchResult[0]);
- $description = mb_substr($description, 0, $length)." ...";
-
- return $description;
- }
-
- //加亮搜索结果中的关键字
- function highLightKeyword($text, $keyword, $color="#C60A00")
- {
- $newword = "$keyword";
- $text = str_replace($keyword, $newword, $text);
- return $text;
- }
-
- //获取文件大小(KB)
- function getFileSize($file)
- {
- $filesize = intval(filesize($file)/1024)."K";
- return $filesize;
- }
-
- //获取文件最后修改的时间
- function getFileTime($file)
- {
- $filetime = date("Y-m-d", filemtime($file));
- return $filetime;
- }
-
- //搜索目录下所有文件
- function searchFile($dir, $keyword)
- {
- $sFile = getFile($dir);
- if (count($sFile)
- return false;
- }
- $sResult = array();
- foreach ($sFile as $file) {
- if (searchText($file, $keyword)) {
- $sResult[] = $file;
- }
- }
- if (count($sResult)
- return false;
- } else {
- return $sResult;
- }
- }
-
- /* 测试代码 */
-
- //指定要搜索的目录
- $dir = "./php_Linux";
- //要搜索的关键字
- $keyword = "sendmail";
-
- $fileArr = searchFile($dir, $keyword);
- $searchSum = count($fileArr);
-
- echo "搜索关键字: $keyword 搜索目录: $dir 搜索结果: $searchSum
";
-
- if ($searchSum
- echo "没有搜索到任何结果";
- } else {
- for
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31