Simple website HTML file search program

Source: Internet
Author: User
Tags filetime php source code

Introduction: this is a simple website HTML file searchProgramThe details page of PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 324005 'rolling = 'no'>

Write it by yourself and simply search forTechnical materials. 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.

ProgramCode:

<? PHP
/**
* 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 for the title of Article
function getfiletitle ($ file, $ default = "None subject ")
{< br> $ 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;
}< BR >}

// 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 ("/(<body. * <\/body>)/is", $ filecontent, $ matchresult );
$ Pattern = array ("/(<[^ \ X80-\ xFF]>)/I", "/(<input. *>)/I ","/(<. *>)/I ","/()/I ","/([<script. *>]). * ([<\/SCRIPT>])/I ","/& amp;/I ","/& quot;/I ","/& #039; /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 = "<font color = $ color> $ keyword </font> ";
$ 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: <B> $ keyword </B> & nbsp; search directory: <B> $ dir </B> & nbsp; search result: <B> $ searchsum </B> <br> <HR size = 1> <br> ";

If ($ searchsum <= 0 ){
Echo "no results found ";
} Else {
Foreach ($ filearr as $ file ){
Echo "<a href = '$ file' target =' _ blank '>". highlightkeyword (getfiletitle ($ file), $ keyword ).
"</A>-". getfilesize ($ file). "& nbsp;". getfiletime ($ file ).
"<Br> \ n <font size = 2>". highlightkeyword (getfiledescribe ($ file), $ keyword ).
"</Font> <br> ";
}
}

?>

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.

More articles on "simple website HTML file search program"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/324005.html pageno: 15.

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.