Home  >  Article  >  php教程  >  php查找文件内容关键字实例代码

php查找文件内容关键字实例代码

WBOY
WBOYOriginal
2016-06-13 10:38:491934browse

  1. /**
  2. * 文件: search.php
  3. * 功能: 搜索指定目录下的HTML文件
  4. */
  5. /* 基本函数 */
  6. //获取目录下文件函数
  7. function getFile($dir)
  8. {
  9.         $dp = opendir($dir);
  10.         $fileArr = array();
  11.         while (!false == $curFile = readdir($dp)) {
  12.                 if ($curFile!="." && $curFile!=".." && $curFile!="") {
  13.                         if (is_dir($curFile)) {
  14.                                 $fileArr = getFile($dir."/".$curFile);
  15.                         } else {
  16.                                 $fileArr[] = $dir."/".$curFile;
  17.                         }
  18.                 }
  19.          }
  20.         return $fileArr;
  21. }
  22. //获取文件内容
  23. function getFileContent($file)
  24. {
  25.         if (!$fp = fopen($file, "r")) {
  26.                 die("Cannot open file $file");
  27.         }
  28.         while ($text = fread($fp, 4096)) {
  29.                 $fileContent .= $text;
  30.         }
  31.         return $fileContent;
  32. }
  33. //搜索指定文件
  34. function searchText($file, $keyword)
  35. {
  36.         $text = getFileContent($file);
  37.         if (preg_match("/$keyword/i", $text)) {
  38.                 return true;
  39.         }
  40.         return false;
  41. }
  42. //搜索出文章的标题
  43. function getFileTitle($file, $default="None subject")
  44. {
  45.         $fileContent = getFileContent($file);
  46.         $sResult = preg_match("/.*/i", $fileContent, $matchResult);
  47.         $title = preg_replace(array("/()/i","/()/i"), "",        $matchResult[0]);
  48.         if (empty($title)) {
  49.                 return $default;
  50.         } else {
  51.                 return $title;
  52.         }
  53. }
  54. //获取文件描述信息
  55. function getFileDescribe($file,$length=200, $default="None describe")
  56. {
  57.         $metas = get_meta_tags($file);
  58.         if ($meta[description] != "") {
  59.                 return $metas[description];
  60.         }
  61.          $fileContent = getFileContent($file);
  62.         preg_match("/()/is", $fileContent, $matchResult);
  63.         $pattern = array("/()/i","/() /i", "/() /i", "/(php查找文件内容关键字实例代码) /i", "/([]) .*([]) /i","/&/i","/"/i","/'/i", "/s/");
  64.         $description = preg_replace($pattern, "", $matchResult[0]);
  65.         $description = mb_substr($description, 0, $length)." ...";
  66.         return $description;
  67. }
  68. //加亮搜索结果中的关键字
  69. function highLightKeyword($text, $keyword, $color="#C60A00")
  70. {
  71.         $newword = "$keyword";
  72.         $text = str_replace($keyword, $newword, $text);
  73.         return $text;
  74. }
  75. //获取文件大小(KB)
  76. function getFileSize($file)
  77. {
  78.         $filesize = intval(filesize($file)/1024)."K";
  79.         return $filesize;
  80. }
  81. //获取文件最后修改的时间
  82. function getFileTime($file)
  83. {
  84.         $filetime = date("Y-m-d", filemtime($file));
  85.         return $filetime;
  86. }
  87. //搜索目录下所有文件
  88. function searchFile($dir, $keyword)
  89. {
  90.         $sFile = getFile($dir);
  91.         if (count($sFile)
  92.                 return false;
  93.         }
  94.         $sResult = array();
  95.         foreach ($sFile as $file) {
  96.                 if (searchText($file, $keyword)) {
  97.                         $sResult[] = $file;
  98.                 }
  99.         }
  100.         if (count($sResult)
  101.                 return false;
  102.         } else {
  103.                 return $sResult;
  104.         }
  105. }
  106. /* 测试代码 */
  107. //指定要搜索的目录
  108. $dir = "./php_Linux";
  109. //要搜索的关键字
  110. $keyword = "sendmail";
  111. $fileArr = searchFile($dir, $keyword);
  112. $searchSum = count($fileArr);
  113. echo "搜索关键字: $keyword   搜索目录: $dir   搜索结果: $searchSum


    ";
  114. if ($searchSum
  115. echo "没有搜索到任何结果";
  116. } else {
  117. for
Statement:
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