我遇到了一个PHP打印语句的问题,该语句连续重复输出大约40或50次,然后停止。我以为它应该只打印一行。我对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);
}
}
?>
对于循环看到的每个文件,都会打印一次"您的搜索没有产生结果"。您应该在呼叫find_files()
:之前进行检查
if (!isset($str_to_find) || empty($str_to_find)) {
print '<p>Your search produced no results</p>';
} else {
find_files('.');
}
然后,您可以从check_files()
中删除该代码位。
您在check_files()
函数中有print语句,该函数是从while...
循环内部调用的。所以,是的,它将在每次循环执行并且条件匹配时执行。
您所说的!==
可能是指!=
?