简单站内HTML文件搜索程序_PHP
自己写来简单的搜索自己电脑上的技术资料的. 大概的思路就是寻找指定目录下的文本, 然后如果有与关键字相匹配的数据就把文件名返回, 最后把所有搜索结果显示出来. 显示结果类似于baidu/google, 呵呵, 这个是纯粹为了好玩.
程序代码:
/**
* 文件: search.php
* 功能: 搜索指定目录下的HTML文件
* 创建: 2005-9-23
* 作者: heiyeluren
*/
/* 基本函数 */
//获取目录下文件函数
function getFile($dir)
{
$dp = opendir($dir);
$fileArr = array();
while (!false == $curFile = readdir($dp)) {
if ($curFile!="." && $curFile!=".." && $curFile!="") {
if (is_dir($curFile)) {
$fileArr = getFile($dir."/".$curFile);
} else {
$fileArr[] = $dir."/".$curFile;
}
}
}
return $fileArr;
}
//获取文件内容
function getFileContent($file)
{
if (!$fp = fopen($file, "r")) {
die("Cannot open file $file");
}
while ($text = fread($fp, 4096)) {
$fileContent .= $text;
}
return $fileContent;
}
//搜索指定文件
function searchText($file, $keyword)
{
$text = getFileContent($file);
if (preg_match("/$keyword/i", $text)) {
return true;
}
return false;
}
//搜索出文章的标题
function getFileTitle($file, $default="None subject")
{
$fileContent = getFileContent($file);
$sResult = preg_match("/
$title = preg_replace(array("/(
if (empty($title)) {
return $default;
} else {
return $title;
}
}
//获取文件描述信息
function getFileDescribe($file,$length=200, $default="None describe")
{
$metas = get_meta_tags($file);
if ($meta['description'] != "") {
return $metas['description'];
}
$fileContent = getFileContent($file);
preg_match("/(
$pattern = array("/()/i","/(
$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) return false;
}
$sResult = array();
foreach ($sFile as $file) {
if (searchText($file, $keyword)) {
$sResult[] = $file;
}
}
if (count($sResult) return false;
} else {
return $sResult;
}
}
/* 测试代码 */
//指定要搜索的目录
$dir = "./php_Linux";
//要搜索的关键字
$keyword = "sendmail";
$fileArr = searchFile($dir, $keyword);
$searchSum = count($fileArr);
echo "搜索关键字: $keyword 搜索目录: $dir 搜索结果: $searchSum
";
if ($searchSum echo "没有搜索到任何结果";
} else {
foreach ($fileArr as $file) {
echo "". highLightKeyword(getFileTitle($file), $keyword) .
" - ".getFileSize($file)." ". getFileTime($file) .
"
\n".highLightKeyword(getFileDescribe($file), $keyword) .
"
";
}
}
?>
完全可以用在自己已经生成静态内容的的搜索, 但是程序效率不高. 如果能够适当的加上 索引/缓存 等机制的话, 我想程序会有趣很多.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

Quark Netdisk and Baidu Netdisk are currently the most commonly used Netdisk software for storing files. If you want to save the files in Quark Netdisk to Baidu Netdisk, how do you do it? In this issue, the editor has compiled the tutorial steps for transferring files from Quark Network Disk computer to Baidu Network Disk. Let’s take a look at how to operate it. How to save Quark network disk files to Baidu network disk? To transfer files from Quark Network Disk to Baidu Network Disk, you first need to download the required files from Quark Network Disk, then select the target folder in the Baidu Network Disk client and open it. Then, drag and drop the files downloaded from Quark Cloud Disk into the folder opened by the Baidu Cloud Disk client, or use the upload function to add the files to Baidu Cloud Disk. Make sure to check whether the file was successfully transferred in Baidu Cloud Disk after the upload is completed. That's it

A file path is a string used by the operating system to identify and locate a file or folder. In file paths, there are two common symbols separating paths, namely forward slash (/) and backslash (). These two symbols have different uses and meanings in different operating systems. The forward slash (/) is a commonly used path separator in Unix and Linux systems. On these systems, file paths start from the root directory (/) and are separated by forward slashes between each directory. For example, the path /home/user/Docume

Recently, many netizens have asked the editor, what is the file hiberfil.sys? Can hiberfil.sys take up a lot of C drive space and be deleted? The editor can tell you that the hiberfil.sys file can be deleted. Let’s take a look at the details below. hiberfil.sys is a hidden file in the Windows system and also a system hibernation file. It is usually stored in the root directory of the C drive, and its size is equivalent to the size of the system's installed memory. This file is used when the computer is hibernated and contains the memory data of the current system so that it can be quickly restored to the previous state during recovery. Since its size is equal to the memory capacity, it may take up a larger amount of hard drive space. hiber

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app
