PHP Glob,排除其名称中包含.files的目录

问题描述:

我使用glob来查找当前索引文件所在的目录中的目录。在它里面我保存从EXCEL中提取的html页面,并且他们创建以.files结尾的文件夹目录有没有办法让所有的目录全局化,但是排除那些以.files结尾的目录?PHP Glob,排除其名称中包含.files的目录

这里是我的代码,因为它是目前:

<?php 
// set to current directory 
$dir = ''; 

// directories only. ignore files, etc. 
foreach(glob($dir.'*', GLOB_ONLYDIR) as $folder){ 
// do not include wordpress directories 
    if (($folder != '*') && ($folder != 'ΦΥΛΑΚΕΣ ΠΡΑΤΗΡΙΩΝ.files') && ($folder != 'wp-includes')) { 
    // list directories and their links 
?> 
    <li><a href="<?php echo $dir."/files/".$folder;?>"><?php echo $folder;?></li> 
<?php 
    } 
} 
?> 

编辑:正如你看到的,我可以排除指定的文件夹,但我想它排除正如我所说的.files自动结束所有文件夹。

您可以使用substr - 函数执行以下操作:

<?php 
// set to current directory 
$dir = ''; 

// directories only. ignore files, etc. 
foreach(glob($dir.'*', GLOB_ONLYDIR) as $folder){ 
// do not include wordpress directories 
    if(($folder != '*') && (substr($folder , -6) != '.files') && ($folder != 'wp-includes')){ 
    // list directories and their links 
?> 
    <li> 
     <a href="<?php echo $dir."/files/".$folder;?>"><?php echo $folder;?></a> 
    </li> 
<?php 
    } 
} 
?> 
+0

这样做的工作,我想要它做我只有一个问题。这只会删除以.files结尾的文件夹?它不会删除我在那里创建的任何其他文件夹? – LamboLighting

+0

是的,这就是我想要的,通过从显示的列表中删除我的意思,我的坏。这正是我想要的!非常感谢! :) – LamboLighting

我更喜欢使用preg_grep功能

$src = glob("*",GLOB_ONLYDIR); 
$nomatch = preg_grep("/.*\.files$|^wp\-includes$/i",$src,PREG_GREP_INVERT); 

foreach($nomatch as $folder) 
{ 
    echo "print ".$folder; 
} 

选择后的格式化是简单

+0

我有点新PHP,所以请原谅我的要求。我的代码的哪一部分替换为您的代码才能使用? – LamboLighting

我难过该消息,我没有如何正确使用它,我在这里写它

<?php 
$cur = getcwd(); 
chdir($dir); 
$src = glob("*",GLOB_ONLYDIR); 
chdir($cur); 
$nomatch = preg_grep("/.*\.files$|^wp\-includes$/i",$src,PREG_GREP_INVERT); 

foreach($nomatch as $folder) 
{ 
?> 
    <li><a href="<?php echo $dir."/files/".$folder;?>"><?php echo $folder;?></li> 
<?php 
} 
?>