2013-03-20 39 views
0

我正在寻找一种方法来测试RecursiveDirectoryIterator对象输出的一部分。 我知道如何识别一个文件夹,但我对PHP很陌生,不熟悉如何查看文件夹对象路径的内容......这可能吗?使用RecursiveDirectoryIterator在目录路径中查找字符串

我有以下几点:

foreach($files as $object){ 
    $indent = str_repeat(' ', $files->getDepth()); 
    if($object->isDir()){ 
     //I know this is wrong... here is where I would want to find the string: 
     if($object contains 'mystring')){ 
      echo "%%%-------found the string!\n"; 
     }else{ 
      echo $indent, "|| dir: $object\n"; 
     } 
    }else{ 
     echo $indent, "-- $object\n"; 
    } 
} 

回答

1

更换线

if($object contains 'mystring')){ 

有:

if(strpos($object->getPathname(), 'mystring') === false){ 

,将搜索对象的完整路径字符串 '了mystring' 和回报boolean false如果'mystring'不是路径的一部分

顺便说一句,PHP中的字符串连接是与点,而不是逗号,所以你可能想要做 echo $indent."-- $object\n";事后

+0

感谢您的帮助。 – jml 2013-03-20 19:11:28

+0

我认为你的意思是'=='而不是'===',对吗?所以不会让我编辑这个改变,因为字符太少了。 – jml 2013-03-22 18:37:29

+1

不,它是===。因为如果搜索项不包含,则strpos返回布尔值false,如果该字符串以搜索项开始,则返回0。因此,如果使用==,即使'mystring'是字符串的一部分,它也可能评估为“false”。看到警告在http://php.net/strpos – periklis 2013-03-22 21:10:39

相关问题