Simple website HTML file search program

Source: Internet
Author: User
Tags filetime
Simple site HTML file search program & nbsp; write your own simple search for technical information on your computer. the general idea is to find the text in the specified directory, and then if you write a simple search for the technical information on your computer. the general idea is to find the text in the specified directory. if there is data that matches the keyword, the file name will be returned, and all the search results will be displayed. the result is similar to baidu/google.

Program code:

/**
* File: search. php
* Function: Search for HTML files in a specified directory
* Creation: 2005-9-23
* Author: heiyeluren
*/


/* Basic functions */

// Obtain the file functions in the 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;
}

// Obtain the 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 a specified object
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>/I ", $ fileContent, $ matchResult );
$ Title = preg_replace (array ("/()/I ","/(<\/title>)/I ")," ", $ matchResult [0]);
If (empty ($ title )){
Return $ default;
} Else {
Return $ title;
}
}

// Obtain the file description
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 ","/([ ]) +. * ([<\/Script>]) +/I ","/&/I ","/"/I", "/'/I ", "/\ s /");
$ Description = preg_replace ($ pattern, "", $ matchResult [0]);
$ Description = mb_substr ($ description, 0, $ length )."...";

Return $ description;
}

// Highlight the keywords in the search results
Function highLightKeyword ($ text, $ keyword, $ color = "# C60A00 ")
{
$ Newword = "$ keyword ";
$ Text = str_replace ($ keyword, $ newword, $ text );
Return $ text;
}

// Get the file size (KB)
Function getFileSize ($ file)
{
$ Filesize = intval (filesize ($ file)/1024). "K ";
Return $ filesize;
}

// Obtain the last modification time of the object
Function getFileTime ($ file)
{
$ Filetime = date ("Y-m-d", filemtime ($ file ));
Return $ filetime;
}

// Search all objects in the 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 be searched
$ Dir = "./php_Linux ";
// The keyword to be searched
$ Keyword = "sendmail ";

$ FileArr = searchFile ($ dir, $ keyword );
$ SearchSum = count ($ fileArr );

Echo "search keyword:$ KeywordSearch Directory:$ DirSearch results:$ SearchSum


";

If ($ searchSum <= 0 ){
Echo "No results found ";
} Else {
Foreach ($ fileArr as $ file ){
Echo "". highLightKeyword (getFileTitle ($ file), $ keyword ).
"-". GetFileSize ($ file). "". getFileTime ($ file ).
"
\ N ". highLightKeyword (getFileDescribe ($ file), $ keyword ).
"

";
}
}

?>

It can be used in searches that have generated static content, but the program efficiency is not high. if you can add an index/Cache mechanism appropriately, I think the program will be much more interesting.

Related Article

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.