我有一个代码来搜索目录中的文件。我正在使用代码var_dump($search->foundFiles)
在网页中显示结果,但我无法弄清楚如何找到正确的代码以便正确显示,结果是网址。
var_dump($search->foundFiles)
的结果是:
数组([0] => archivednews / 2016-01-08 22h30 Zika Virus归档news.html [1] => archivednews / 2016-01-07 22h30 Zika Virus archived news.html)
但是我希望它显示在一个列表中,其中包含指向找到的文件的可点击链接:
<ul>
<li><a href="archivednews/2016-01-05 22h30 Zika Virus archived news.html">2016-01-05 22h30 Zika Virus archived news.html</a></li>
<li><a href="archivednews/2016-01-04 22h30 Zika Virus archived news.html">2016-01-04 22h30 Zika Virus archived news.html</a></li>
<li><a href="archivednews/2016-01-08 22h30 Zika Virus archived news.html">2016-01-08 22h30 Zika Virus archived news.html</a></li>
</ul>
这是完整的代码:
class searchFileContents{
var $dir_name = '';//The directory to search
var $search_phrase = '';//The phrase to search in the file contents
var $allowed_file_types = array('php','phps');//The file types that are searched
var $foundFiles;//Files that contain the search phrase will be stored here
var $myfiles;
function search($directory, $search_phrase){
$this->dir_name = $directory;
$this->search_phrase = $search_phrase;
$this->myfiles = $this->GetDirContents($this->dir_name);
$this->foundFiles = array();
if ( empty($this->search_phrase) ) die('Empty search phrase');
if ( empty($this->dir_name) ) die('You must select a directory to search');
foreach ( $this->myfiles as $f ){
if ( in_array(array_pop(explode ( '.', $f )), $this->allowed_file_types) ){
$contents = file_get_contents($f);
if ( strpos($contents, $this->search_phrase) !== false )
$this->foundFiles [] = $f;
}
}
return $this->foundFiles;
}
function GetDirContents($dir){
if (!is_dir($dir)){die ("Function GetDirContents: Problem reading : $dir!");}
if ($root=@opendir($dir)){
while ($file=readdir($root)){
if($file=="." || $file==".."){continue;}
if(is_dir($dir."/".$file)){
$files=array_merge($files,$this->GetDirContents($dir."/".$file));
}else{
$files[]=$dir."/".$file;
}
}
}
return $files;
}
}
//Example :
$search = new searchFileContents;
$search->search('E:/htdocs/AccessClass', 'class');
var_dump($search->foundFiles);
答案 0 :(得分:1)
var_dump
仅在开发时使用。它只是调试的帮手。
应用程序完成后,您无法将其输出给用户。
您可以像var_dump一样打印数组的每个值:
foreach($search->foundFiles as $ffiles)
echo "<a href='$ffiles'>$ffiles</a><br>";
这会显示:
2016-01-05 22h30 Zika Virus archived news.html
2016-01-04 22h30 Zika Virus archived news.html
2016-01-08 22h30 Zika Virus archived news.html
但阵列中的网址不是真正的链接,所以你必须解决这个问题。