PHP search file content keyword example code_PHP tutorial
Release: 2016-07-13 17:40:20
Original
1452 people have browsed it
- /**
- * File: search.php
- * Function: Search for HTML files in the specified directory
- */
- /* Basic functions*/
- //Get the file function in the directory
- function getFile($dir)
- {
- $dp = opendir($dir);
- $fileArr = array();
- while (!false == $curFile = readdir($dp)) {
- if ($curFile!="." && $curFile!=".. " && $curFile!="") {
- if (is_dir($curFile)) {
- File);
- }
- }
- }
- return $fileArr;
- }
- //Get file content
- function getFileContent($file)
- {
- if (!$fp = fopen($file, "r")) {
- die("Cannot open file $file");
- }
- while ($text = fread($fp, 4096)) {
- $fileContent .= $text;
- }
- return $fileContent;
- }
- //Search for the specified file
- function searchText($file, $keyword)
- {
- $text = getFileContent($file);
- if (preg_match("/$keyword/i", $text)) {
- return true;
- }
- return false; subject")
- {
- $fileContent = getFileContent($file);
- $sResult = preg_match("/.*/ i", $fileContent, $matchResult);
- $title = preg_replace(array("/()/i","/()/i"), " ", $matchResult[0]); return $title;
- }
- }
-
- //Get file description information
- function getFileDescribe($file,$length= 200, $default="None describe")
- {
- $metas = get_meta_tags($file);
- if ($meta[description] != "") {
- return $metas[description];
- }
- $fileContent = getFileContent($file);
- preg_match("/()/is", $fileContent, $matchResult);
- $pattern = array("/(<[^x80-xff] >)/i","/() /i", "/() /i", "/() /i", "/([]) .*([]) /i","/&/i","/"/i","/'/i", "/s/");
- $description = preg_replace($pattern, "", $matchResult[0]);
- $description = mb_substr($description, 0, $length)." ...";
-
- return $description;
- }
-
- //加亮搜索结果中的关键字
- function highLightKeyword($text, $keyword, $color="#C60A00")
- {
- $newword = "$keyword";
- $text = str_replace($keyword, $newword, $text);
- return $text;
- }
-
- //获取文件大小(KB)
- function getFileSize($file)
- {
- $filesize = intval(filesize($file)/1024)."K";
- return $filesize;
- }
-
- //获取文件最后修改的时间
- function getFileTime($file)
- {
- $filetime = date("Y-m-d", filemtime($file));
- return $filetime;
- }
-
- //搜索目录下所有文件
- function searchFile($dir, $keyword)
- {
- $sFile = getFile($dir);
- if (count($sFile) <= 0) {
- return false;
- }
- $sResult = array();
- foreach ($sFile as $file) {
- if (searchText($file, $keyword)) {
- $sResult[] = $file;
- }
- }
- if (count($sResult) <= 0) {
- return false;
- } else {
- return $sResult;
- }
- }
- /* 测试代码 */
- //指定要搜索的目录
- $dir = "./php_Linux";
- //要搜索的关键字
- $keyword = "sendmail";
- $fileArr = searchFile($dir, $keyword);
- $searchSum = count($fileArr);
- echo "搜索关键字: $keyword 搜索目录: $dir 搜索结果: $searchSum
";
-
- if ($searchSum <= 0) {
- echo "没有搜索到任何结果";
- } else {
- 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*/ /* 基本函数 */ //获取目录下文件函数 function getFile($dir) { $dp = opendir($dir); $fileAr...
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31