<?php
/**
* File: search.php
* Function: Search the HTML file under the specified directory
*/
/* Basic function */
Get file functions under 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 files
function SearchText ($file, $keyword)
{
$text = Getfilecontent ($file);
if (Preg_match ("/$keyword/I", $text)) {
return true;
}
return false;
}
Search out 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 ","/& #039;/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 keyword search: <b> $keyword </b> Search Catalog: <b> $dir </b> Search Result: <b> $searchSum </b><br>
if ($searchSum <= 0) {
echo "No results found";
} else {
For