Simple Site HTML File search program

Source: Internet
Author: User
Tags foreach array count file size filetime return
The program writes itself to simply search for the technical information on your computer. The general idea is to look for the text in the specified directory, and then return the file name if it matches the keyword, and then display all the search results. Display results similar to Baidu/google, hehe, this is purely for fun.

Program code:

<?php
/**
* File: search.php
* Function: Search for HTML files in the specified directory
* Created: 2005-9-23
* Author: Heiyeluren */


/* Basic Function * *

Get file Functions in Directory
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;
}

Get file Contents
function Getfilecontent ($file)
{
if (! $fp = fopen ($file, "R")) {
Die ("Cannot open file $file");
}
while ($text = Fread ($fp, 4096)) {
$fileContent. = $text;
}
return $fileContent;
}

Search for specified file
function SearchText ($file, $keyword)
{
$text = Getfilecontent ($file);
if (Preg_match ("/$keyword/I", $text)) {
return true;
}
return false;
}

Search the title of the article
function GetFileTitle ($file, $default = "None subject")
{
$fileContent = Getfilecontent ($file);
$sResult = Preg_match ("/<title>.*<\/title>/i", $fileContent, $matchResult);
$title = preg_replace (Array ("/(<title>)/I", "/(<\/title>)/I"), "", $matchResult [0]);
if (empty ($title)) {
return $default;
} else {
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 ("/(<body.*<\/body>)/is", $fileContent, $matchResult);
$pattern = Array ("/(<[^\x80-\xff]+>)/I", "/(<input.*>) +/i", "/(<a.*>) +/i", "/() +/i", "/([<script.*>]) +.* ([<\/script>]) +/i", "/&/i", "/"/I ","/'/I ","/\s/");
$description = Preg_replace ($pattern, "", $matchResult [0]);
$description = mb_substr ($description, 0, $length). "...";

return $description;
}

Highlight keywords in search results
function Highlightkeyword ($text, $keyword, $color = "#C60A00")
{
$newword = "<font color= $color > $keyword </font>";
$text = Str_replace ($keyword, $newword, $text);
return $text;
}

Get file Size (KB)
function GetFileSize ($file)
{
$filesize = Intval (filesize ($file)/1024). " K ";
return $filesize;
}

Gets the time the file was last modified
function Getfiletime ($file)
{
$filetime = Date ("y-m-d", Filemtime ($file));
return $filetime;
}

Search all Files under directory
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;
}
}


/* Test Code * *

Specify the directory to search
$dir = "./php_linux";
Keywords to search for
$keyword = "SendMail";

$FILEARR = Searchfile ($dir, $keyword);
$searchSum = count ($FILEARR);

echo Search keywords: <b> $keyword </b> Search directory: <b> $dir </b> search results: <b> $searchSum </b><br>

if ($searchSum <= 0) {
echo "did not search for any results";
} else {
foreach ($fileArr as $file) {
echo "<a href= ' $file ' target= ' _blank ' > '. Highlightkeyword (GetFileTitle ($file), $keyword).
"</a>-". GetFileSize ($file). " ". Getfiletime ($file).
"<br>\n<font size=2>". Highlightkeyword (Getfiledescribe ($file), $keyword).
"</font><br><br>";
}
}

?>

You can use a search that already generates static content, but the program is inefficient. I think the program would be a lot more interesting if you could properly add a mechanism such as indexing/caching.



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.