找到你要的答案

Q:PHP - Read few files

Q:PHP读几个文件

    $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);

I want to read all the files which is between $a and $b.

If $a is 3 and $b is 5, then read chapter3.html, chapter4.html, chapter5.html

But $it is null. I feel that I am missing something. Can you please help?

var_dump on each statement returns

$output dir looks like

    <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>

Directory looks good

Recursive directory iterator returns

     <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>

Directory looks invalid

Recursive iterator iterator returns

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

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

What does this two mean?

Regex is

   </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>

Regex iterator is

    <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>
    $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之间的文件。

如果是3美元美元和B 5,然后读chapter3.html,chapter4.html,chapter5.html

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

在每个语句var_dump返回

为输出目录看起来像

    <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>
answer1: 回答1:

Regex doesn't look correct to me. I think it should be:

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

正则表达式看起来不正确的我。我认为应该是:

$regex = '/Chapter{' .$a. ',' .$b. '}\.html$/';
answer2: 回答2:

Like this you can get the path(s) to the desired files:

 $a=3;
 $b=5;

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

dirname(__FILE__) is to go from the place where this file is. So my test scenario looked like this

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

这样你就可以得到路径到所需的文件:

 $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
php  file