Php search file content keyword instance code

Source: Internet
Author: User
Tags filetime

  1. <? Php
  2. /**
  3. * File: search. php
  4. * Function: Search for HTML files in a specified directory
  5. */
  6. /* Basic functions */
  7. // Obtain the file functions in the directory
  8. Function getFile ($ dir)
  9. {
  10. $ Dp = opendir ($ dir );
  11. $ FileArr = array ();
  12. While (! False = $ curFile = readdir ($ dp )){
  13. If ($ curFile! = "." & $ CurFile! = "..." & $ CurFile! = ""){
  14. If (is_dir ($ curFile )){
  15. $ FileArr = getFile ($ dir. "/". $ curFile );
  16. } Else {
  17. $ FileArr [] = $ dir. "/". $ curFile;
  18. }
  19. }
  20. }
  21. Return $ fileArr;
  22. }
  23. // Obtain the File Content
  24. Function getFileContent ($ file)
  25. {
  26. If (! $ Fp = fopen ($ file, "r ")){
  27. Die ("Cannot open file $ file ");
  28. }
  29. While ($ text = fread ($ fp, 4096 )){
  30. $ FileContent. = $ text;
  31. }
  32. Return $ fileContent;
  33. }
  34. // Search for a specified object
  35. Function searchText ($ file, $ keyword)
  36. {
  37. $ Text = getFileContent ($ file );
  38. If (preg_match ("/$ keyword/I", $ text )){
  39. Return true;
  40. }
  41. Return false;
  42. }
  43. // Search the title of the article
  44. Function getFileTitle ($ file, $ default = "None subject ")
  45. {
  46. $ FileContent = getFileContent ($ file );
  47. $ SResult = preg_match ("/<title>. * </title>/I", $ fileContent, $ matchResult );
  48. $ Title = preg_replace (array ("/(<title>)/I", "/(</title>)/I "),"", $ matchResult [0]);
  49. If (empty ($ title )){
  50. Return $ default;
  51. } Else {
  52. Return $ title;
  53. }
  54. }
  55. // Obtain the file description
  56. Function getFileDescribe ($ file, $ length = 200, $ default = "None describe ")
  57. {
  58. $ Metas = get_meta_tags ($ file );
  59. If ($ meta [description]! = ""){
  60. Return $ metas [description];
  61. }
  62. $ FileContent = getFileContent ($ file );
  63. Preg_match ("/(<body. * </body>)/is", $ fileContent, $ matchResult );
  64. $ Pattern = array ("/(<^ x80-xff]>)/I", "/(<input. *>)/I ","/(<. *>)/I ","/()/I ","/([<script. *>]). * ([</script>])/I ","/& amp;/I ","/& quot;/I ","/& #039; /I ","/s /");
  65. $ Description = preg_replace ($ pattern, "", $ matchResult [0]);
  66. $ Description = mb_substr ($ description, 0, $ length )."...";
  67. Return $ description;
  68. }
  69. // Highlight the keywords in the search results
  70. Function highLightKeyword ($ text, $ keyword, $ color = "# C60A00 ")
  71. {
  72. $ Newword = "<font color = $ color> $ keyword </font> ";
  73. $ Text = str_replace ($ keyword, $ newword, $ text );
  74. Return $ text;
  75. }
  76. // Get the file size (KB)
  77. Function getFileSize ($ file)
  78. {
  79. $ Filesize = intval (filesize ($ file)/1024). "K ";
  80. Return $ filesize;
  81. }
  82. // Obtain the last modification time of the object
  83. Function getFileTime ($ file)
  84. {
  85. $ Filetime = date ("Y-m-d", filemtime ($ file ));
  86. Return $ filetime;
  87. }
  88. // Search all objects in the directory
  89. Function searchFile ($ dir, $ keyword)
  90. {
  91. $ SFile = getFile ($ dir );
  92. If (count ($ sFile) <= 0 ){
  93. Return false;
  94. }
  95. $ SResult = array ();
  96. Foreach ($ sFile as $ file ){
  97. If (searchText ($ file, $ keyword )){
  98. $ SResult [] = $ file;
  99. }
  100. }
  101. If (count ($ sResult) <= 0 ){
  102. Return false;
  103. } Else {
  104. Return $ sResult;
  105. }
  106. }
  107. /* Test code */
  108. // Specify the directory to be searched
  109. $ Dir = "./php_Linux ";
  110. // The keyword to be searched
  111. $ Keyword = "sendmail ";
  112. $ FileArr = searchFile ($ dir, $ keyword );
  113. $ SearchSum = count ($ fileArr );
  114. Echo "Search keyword: <B> $ keyword </B> & nbsp; search directory: <B> $ dir </B> & nbsp; search result: <B> $ searchSum </B> <br>
  115. If ($ searchSum <= 0 ){
  116. Echo "no results found ";
  117. } Else {
  118. For

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.