PHP Find file content keyword Instance code

Source: Internet
Author: User
Tags filetime


    1. <?php

    2. /**

    3. * File: search.php

    4. * Function: Search the HTML file under the specified directory

    5. */

    6. /* Basic function */


    7. Get file functions under 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. Get file Contents

    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 specified files

    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 out 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. Get File description Information

    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", "/(<a.*>)/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 keywords in 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 file Size (KB)

    77. function GetFileSize ($file)

    78. {

    79. $filesize = Intval (filesize ($file)/1024). " K ";

    80. return $filesize;

    81. }


    82. Gets the time the file was last modified

    83. function Getfiletime ($file)

    84. {

    85. $filetime = Date ("y-m-d", Filemtime ($file));

    86. return $filetime;

    87. }


    88. Search all Files under 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 search

    109. $dir = "./php_linux";

    110. Keywords to search for

    111. $keyword = "SendMail";


    112. $FILEARR = Searchfile ($dir, $keyword);

    113. $searchSum = count ($FILEARR);


    114. echo keyword search: <b> $keyword </b> &nbsp; Search Catalog: <b> $dir </b> &nbsp; Search Result: <b> $searchSum </b><br>


    115. if ($searchSum <= 0) {

    116. echo "No results found";

    117. } else {

    118. For


PHP Find file content keyword Instance code

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.