PHP - 阅读几个文件

时间:2015-10-08 10:21:38

标签: php file

    $outputDir = $_SERVER['DOCUMENT_ROOT'] . "/dirName";

    $dir    = new RecursiveDirectoryIterator($outputDir);
    $rec    = new RecursiveIteratorIterator($dir);
    $a=3;
    $b = 10;
    $regex = '/Chapter[' .$a. ',' .$b. '/.html]$/';
    $it = new RegexIterator($rec, $regex);

我想阅读介于$ a和$ b之间的所有文件。

如果$ a为3且$ b为5,则请阅读chapter3.html, chapter4.html, chapter5.html

但是$它是空的。我觉得我错过了什么。你能帮忙吗?

每个语句的var_dump返回

$ output dir看起来像

    <pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font 
    color='#cc0000'>'C:/server/httpd/Apache24/htdocs//front-page1//front-
     page//subjects//maths'</font> <i>(length=73)</i>
    </pre>

目录看起来不错

递归目录迭代器返回

     <b>object</b>(<i>RecursiveDirectoryIterator</i>)[<i>1</i>]
     <i>private</i> 'pathName' <small>(SplFileInfo)</small> <font  
       color='#888a85'>=&gt;</font> <small>string</small> <font 
       color='#cc0000'>'C:/server/httpd/Apache24/htdocs//front-page1//front-
       page//subjects//maths\.'</font> <i>(length=75)</i>

     <i>private</i> 'fileName' <small>(SplFileInfo)</small> <font 
       color='#888a85'>=&gt;</font> <small>string</small> <font 
       color='#cc0000'>'.'</font> <i>(length=1)</i>

     <i>private</i> 'glob' <small>(DirectoryIterator)</small> <font 
       color='#888a85'>=&gt;</font> <small>boolean</small> <font    
       color='#75507b'>false</font>

    <i>private</i> 'subPathName' <font color='#888a85'>=&gt;</font> 
    <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
    </pre>

目录看起来无效

递归迭代器迭代器返回

    <pre class='xdebug-var-dump' dir='ltr'>

    <b>object</b>(<i>RecursiveIteratorIterator</i>)[<i>2</i>]

这两个意味着什么?

正则表达式

   </pre><pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font 
     color='#cc0000'>'/Chapter{0,5}\.html$/i'</font> <i>(length=22)</i>
   </pre>

正则表达式迭代器是

    <pre class='xdebug-var-dump' dir='ltr'>

       <b>object</b>(<i>RegexIterator</i>)[<i>3</i>]
       <i>public</i> 'replacement' <font color='#888a85'>=&gt;</font> <font  
          color='#3465a4'>null</font>
    </pre>

2 个答案:

答案 0 :(得分:0)

正则表达式对我来说不正确。我认为应该是:

$regex = '/Chapter{' .$a. ',' .$b. '}\.html$/';

答案 1 :(得分:0)

像这样你可以获得所需文件的路径:

 $a=3;
 $b=5;

var_dump(glob(dirname(__FILE__).'/test/chapter*['.$a.'-'.$b.'].html')) ;

dirname(__FILE__)将从此文件所在的位置开始。所以我的测试场景看起来像这样

|____index.php
|____test
| |____chapter1.html
| |____chapter2.html
| |____chapter3.html
| |____chapter4.html
| |____chapter5.html
| |____chapter6.html