I wrote a simple search for the technical materials on my 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. show similar results
I wrote a simple search for the technical materials on my 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 display result is similar to baidu/google.
Program code:
/**
* File: search. php
* Function: Search for HTML files in a specified directory.
* Created: 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 ')
{
$ 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 ')
{
$ 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 ', '/\ s /');
$ Description = preg_replace ($ pattern, '', $ matchResult [0]);
$ Description = mb_substr ($ description, 0, $ length ).'...';
Return $ description;
}
// Highlight key words in search results
Function highLightKeyword ($ text, $ keyword, $ color = '# C60A00 ')
{
$ Newword = '$ keyword ';
$ Text = str_replace ($ keyword, $ newword, $ text );