Php search file content keyword instance code _ PHP Tutorial

Source: Internet
Author: User
Tags filetime
Php search file content keyword instance code .? Php *** File: search. php * function: Search for the HTML file in the specified directory ** basic function * to obtain the file function functiongetFile ($ dir) {$ dpopendir ($ dir); $ fileAr

  1. /**
  2. * File: search. php
  3. * Function: Search for HTML files in a specified directory
  4. */
  5. /* Basic functions */
  6. // Obtain the file functions 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. $ FileArr = getFile ($ dir. "/". $ curFile );
  15. } Else {
  16. $ FileArr [] = $ dir. "/". $ curFile;
  17. }
  18. }
  19. }
  20. Return $ fileArr;
  21. }
  22. // Obtain the file content
  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 a specified object
  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 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. // Obtain the file description
  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 ("/(<[^ x80-xff]>)/I ","/( )/I ","/([ ]). * ([Script])/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 the keywords in the 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 the file size (KB)
  76. Function getFileSize ($ file)
  77. {
  78. $ Filesize = intval (filesize ($ file)/1024). "K ";
  79. Return $ filesize;
  80. }
  81. // Obtain the last modification time of the object
  82. Function getFileTime ($ file)
  83. {
  84. $ Filetime = date ("Y-m-d", filemtime ($ file ));
  85. Return $ filetime;
  86. }
  87. // Search all objects in the 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 be searched
  108. $ Dir = "./php_Linux ";
  109. // The keyword to be searched
  110. $ Keyword = "sendmail ";
  111. $ FileArr = searchFile ($ dir, $ keyword );
  112. $ SearchSum = count ($ fileArr );
  113. Echo "search keyword:$ KeywordSearch Directory:$ DirSearch results:$ SearchSum

    ";
  114. If ($ searchSum <= 0 ){
  115. Echo "No results found ";
  116. } Else {
  117. For

    Http://www.bkjia.com/PHPjc/486221.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486221.htmlTechArticle? Php/*** File: search. php * function: Search for HTML files in the specified directory * // * Basic functions * // Obtain the function getFile ($ dir) of the file in the directory) {$ 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.