This article describes how to search for a file in php and list a code example of the file name to be searched. First, let's look at a simple php search file and the code displayed. As follows:
The following are more powerful and complex features.
. * <\/Title>/I ", $ fileContent, $ matchResult); $ title = preg_replace (array ("/()/I ","/(<\/title>)/I ")," ", $ matchResult [0]); if (empty ($ title )) {return $ default;} else {return $ title ;}// 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 keyword 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 file. function getFileTime ($ file) {$ filetime = date ("Y-m-d ", filemtime ($ file); return $ filetime;} // search all files 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 ;}} // by bbs.it-home.org/* 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 )."
";}}?>
|