2012-02-24 89 views
0

我有,不斷地重複輸出約40或50倍PHP print語句的問題,然後停止。我以爲它應該只打印一行。我對PHP還是比較陌生,所以我不明白我做錯了什麼。相關代碼位於代碼片段的底部。PHP重複打印語句

在此先感謝.....

<?php 
$query = $_POST['query']; 
find_files('.'); 

function find_files($seed) { 
    if(! is_dir($seed)) return false; 

    $files = array(); 
    $dirs = array($seed); 

    while(NULL !== ($dir = array_pop($dirs))) { 
     if($dh = opendir($dir)) { 
      while(false !== ($file = readdir($dh))) { 
       if($file == '.' || $file == '..') continue; 
       $path = $dir . '/' . $file; 
       if(is_dir($path)) { 
        $dirs[] = $path; 
       } else { 
        if(preg_match('/^.*\.(php[\d]?|js|txt)$/i', $path)) { 
         check_files($path); 
        } 
       } 
      } 
      closedir($dh); 
     } 
    } 
} 

function check_files($this_file) { 
    $query = $_POST['query']; 
    $str_to_find = $query; 

    if ((isset($str_to_find)) && (empty($str_to_find))) { 
     print '<p>Your search produced no results</p>'; 
    } else { 
     if(!($content = file_get_contents($this_file))) { 
      echo("<p>Could not check $this_file</p>\n"); 
     } else { 
      if(stristr($content, $str_to_find)) { 
       echo("<p>$this_file -> contains $str_to_find</p>\n"); 
      } 
     } 
     unset($content); 
    } 
} 
?> 
+0

它是「你的搜尋無」那是重複? – spencercw 2012-02-24 23:33:25

+0

是的,這是正確的 – 2012-02-24 23:33:42

+0

也許您呼叫Checkfiles反覆(在由spencercw暗示)? - > check_files($路徑);它處於while()循環中,所以每次循環時都會打印。 – user978122 2012-02-24 23:36:29

回答

2

「你的搜尋無」會爲每一個你的循環看到文件一旦被打印出來。你叫find_files()之前,您應該做的檢查:

if (!isset($str_to_find) || empty($str_to_find)) { 
    print '<p>Your search produced no results</p>'; 
} else { 
    find_files('.'); 
} 

然後,您可以從check_files()刪除的代碼位。

+0

spencercw ......感謝在這一個幫助。非常好的工作 – 2012-02-24 23:56:29

0

你有check_files()功能,正在從while...循環內稱爲內的打印語句。所以,是的,每次循環執行和條件匹配時都會執行。

0

通過!==你也許意味着!=

+0

'==='只是意味着'完全相等',並不會像將字符串轉換爲整數一樣。總是使用'==='和'!=='是一個好主意。 – spencercw 2012-02-24 23:44:39