Php search file content keyword instance code .? Php *** File: search. php * function: Search for the HTML file in the specified directory ** basic function * to obtain the file function functiongetFile ($ dir) {$ dpopendir ($ dir); $ fileAr
-
- /**
- * File: search. php
- * Function: Search for HTML files in a specified directory
- */
- /* 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 ("/.*/I ", $ fileContent, $ matchResult );
- $ Title = preg_replace (array ("/()/I ","/()/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 {
- For
Http://www.bkjia.com/PHPjc/486221.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486221.htmlTechArticle? Php/*** File: search. php * function: Search for HTML files in the specified directory * // * Basic functions * // Obtain the function getFile ($ dir) of the file in the directory) {$ dp = opendir ($ dir); $ fileAr...