PHP code reference for searching files and listing file names

Source: Internet
Author: User
Tags filetime
PHP code reference for searching files and listing file names
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 )."

";}}?>

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.