Home php教程 php手册 php查找文件内容关键字实例代码

php查找文件内容关键字实例代码

Jun 13, 2016 am 10:38 AM
php code Keywords content Function Example designation search document Find of

  1. /**
  2. * 文件: search.php
  3. * 功能: 搜索指定目录下的HTML文件
  4. */
  5. /* 基本函数 */
  6. //获取目录下文件函数
  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. //获取文件内容
  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. //搜索指定文件
  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. //搜索出文章的标题
  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. //获取文件描述信息
  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("/()/i","/() /i", "/() /i", "/(php查找文件内容关键字实例代码) /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. //加亮搜索结果中的关键字
  69. function highLightKeyword($text, $keyword, $color="#C60A00")
  70. {
  71.         $newword = "$keyword";
  72.         $text = str_replace($keyword, $newword, $text);
  73.         return $text;
  74. }
  75. //获取文件大小(KB)
  76. function getFileSize($file)
  77. {
  78.         $filesize = intval(filesize($file)/1024)."K";
  79.         return $filesize;
  80. }
  81. //获取文件最后修改的时间
  82. function getFileTime($file)
  83. {
  84.         $filetime = date("Y-m-d", filemtime($file));
  85.         return $filetime;
  86. }
  87. //搜索目录下所有文件
  88. function searchFile($dir, $keyword)
  89. {
  90.         $sFile = getFile($dir);
  91.         if (count($sFile)
  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)
  101.                 return false;
  102.         } else {
  103.                 return $sResult;
  104.         }
  105. }
  106. /* 测试代码 */
  107. //指定要搜索的目录
  108. $dir = "./php_Linux";
  109. //要搜索的关键字
  110. $keyword = "sendmail";
  111. $fileArr = searchFile($dir, $keyword);
  112. $searchSum = count($fileArr);
  113. echo "搜索关键字: $keyword   搜索目录: $dir   搜索结果: $searchSum


    ";
  114. if ($searchSum
  115. echo "没有搜索到任何结果";
  116. } else {
  117. for
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles