PHP Find file Content keyword Instance Code _php tutorial

Source: Internet
Author: User
Tags filetime
  1. /**
  2. * File: search.php
  3. * Function: Search the HTML file under the specified directory
  4. */
  5. /* Basic function */
  6. Get file functions under 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. $FILEARR = GetFile ($dir. " /". $curFile);
  15. } else {
  16. $FILEARR [] = $dir. " /". $curFile;
  17. }
  18. }
  19. }
  20. return $FILEARR;
  21. }
  22. Get file Contents
  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. Search for specified files
  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. Search out the title of the article
  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. Get File description Information
  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 ("/( <> < body=""> )/is", $fileContent, $matchResult);<>
  63. $pattern = Array ("/(<[^x80-xff] >)/I", "/()/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. Highlight keywords in search results
  69. function Highlightkeyword ($text, $keyword, $color = "#C60A00")
  70. {
  71. $newword = "$keyword";
  72. $text = Str_replace ($keyword, $newword, $text);
  73. return $text;
  74. }
  75. Get file Size (KB)
  76. function GetFileSize ($file)
  77. {
  78. $filesize = Intval (filesize ($file)/1024). " K ";
  79. return $filesize;
  80. }
  81. Gets the time the file was last modified
  82. function Getfiletime ($file)
  83. {
  84. $filetime = Date ("y-m-d", Filemtime ($file));
  85. return $filetime;
  86. }
  87. Search all Files under directory
  88. function Searchfile ($dir, $keyword)
  89. {
  90. $sFile = GetFile ($dir);
  91. if (count ($sFile) <= 0) {
  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) <= 0) {
  101. return false;
  102. } else {
  103. return $sResult;
  104. }
  105. }
  106. /* Test Code */
  107. Specify the directory to search
  108. $dir = "./php_linux";
  109. Keywords to search for
  110. $keyword = "SendMail";
  111. $FILEARR = Searchfile ($dir, $keyword);
  112. $searchSum = count ($FILEARR);
  113. echo Search keyword: $keyword Search catalog: $dir search results: $searchSum

    ";
  114. if ($searchSum <= 0) {
  115. echo "No results found";
  116. } else {
  117. For

    http://www.bkjia.com/PHPjc/486221.html www.bkjia.com true http://www.bkjia.com/PHPjc/486221.html techarticle PHP/** * File: search.php * Function: Search HTML file under the specified directory */* Basic function *//Get directory under File functions function GetFile ($dir) {$DP = Opendir ($dir); $fileAr ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.