Home >Backend Development >PHP Tutorial >PHP search file content keyword example code_PHP tutorial

PHP search file content keyword example code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:40:201456browse

  1. /**
  2. * File: search.php
  3. * Function: Search for HTML files in the specified directory
  4. */
  5. /* Basic functions*/
  6. //Get the file function in the directory
  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.                                                                        File);
  15.                                                                                                               }
  16. }
  17. }
  18.         return $fileArr;
  19. }
  20. //Get file content
  21. function getFileContent($file)
  22. {
  23. if (!$fp = fopen($file, "r")) {
  24. die("Cannot open file $file");
  25. }
  26. while ($text = fread($fp, 4096)) {
  27. $fileContent .= $text;
  28. }
  29. return $fileContent;
  30. }
  31. //Search for the specified file
  32. function searchText($file, $keyword)
  33. {
  34. $text = getFileContent($file);
  35. if (preg_match("/$keyword/i", $text)) {
  36. return true;
  37. }
  38.           return false; subject")
  39. {
  40. $fileContent = getFileContent($file);
  41. $sResult = preg_match("/.*/ i", $fileContent, $matchResult);
  42. $title = preg_replace(array("/()/i","/()/i"), " ",         $matchResult[0]); return $title;
  43. }
  44. }
  45. //Get file description information
  46. function getFileDescribe($file,$length= 200, $default="None describe")
  47. {
  48. $metas = get_meta_tags($file);
  49. if ($meta[description] != "") {
  50. return $metas[description];
  51. }
  52.          $fileContent = getFileContent($file);
  53.         preg_match("/()/is", $fileContent, $matchResult);
  54.         $pattern = array("/(<[^x80-xff] >)/i","/() /i", "/() /i", "/() /i", "/([]) .*([]) /i","/&/i","/"/i","/'/i", "/s/");
  55.         $description = preg_replace($pattern, "", $matchResult[0]);
  56.         $description = mb_substr($description, 0, $length)." ...";
  57.         return $description;
  58. }
  59. //加亮搜索结果中的关键字
  60. function highLightKeyword($text, $keyword, $color="#C60A00")
  61. {
  62.         $newword = "$keyword";
  63.         $text = str_replace($keyword, $newword, $text);
  64.         return $text;
  65. }
  66. //获取文件大小(KB)
  67. function getFileSize($file)
  68. {
  69.         $filesize = intval(filesize($file)/1024)."K";
  70.         return $filesize;
  71. }
  72. //获取文件最后修改的时间
  73. function getFileTime($file)
  74. {
  75.         $filetime = date("Y-m-d", filemtime($file));
  76.         return $filetime;
  77. }
  78. //搜索目录下所有文件
  79. function searchFile($dir, $keyword)
  80. {
  81.         $sFile = getFile($dir);
  82.         if (count($sFile) <= 0) {
  83.                 return false;
  84.         }
  85.         $sResult = array();
  86.         foreach ($sFile as $file) {
  87.                 if (searchText($file, $keyword)) {
  88.                         $sResult[] = $file;
  89.                 }
  90.         }
  91.         if (count($sResult) <= 0) {
  92.                 return false;
  93.         } else {
  94.                 return $sResult;
  95.         }
  96. }
  97. /* 测试代码 */
  98. //指定要搜索的目录
  99. $dir = "./php_Linux";
  100. //要搜索的关键字
  101. $keyword = "sendmail";
  102. $fileArr = searchFile($dir, $keyword);
  103. $searchSum = count($fileArr);
  104. echo "搜索关键字: $keyword   搜索目录: $dir   搜索结果: $searchSum


    ";
  105. if ($searchSum <= 0) {
  106. echo "没有搜索到任何结果";
  107. } else {
  108. for

    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/486221.htmlTechArticle?php /*** File: search.php * Function: Search for HTML files in the specified directory*/ /* 基本函数 */ //获取目录下文件函数 function getFile($dir) { $dp = opendir($dir); $fileAr...
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