我有一個代碼來搜索目錄中的文件。我使用代碼var_dump($search->foundFiles)
在網頁上顯示結果,但我無法弄清楚如何找到正確的代碼以便正確顯示,結果是URL。我怎樣樣式通過var_dump導致數組的顯示
的var_dump($search->foundFiles)
結果是:
陣列([0] => archivednews/2016年1月8日22:30寨卡病毒存檔news.html [1] => archivednews/2016年1月7日22:30茲卡病毒存檔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 ([email protected]($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);
'var_dump'具有固定的格式。做「foreach」和「echo」值。 –