PHP如何获取所有子目录中的所有文件(仅限html文件)并索引每个html页面

时间:2011-06-27 05:10:26

标签: php html directory indexing

对于家庭作业,我必须在当前和所有子目录中获取所有.htm和.html文件,并且我必须通过计算单独出现在文件中的所有单词来索引它们。

以下是我在目录中找到html文件后对文件进行计数的方法:

$file = '.html';
$index = indexer($file);
echo '<pre>'.print_r($index,true).'</pre>';

function indexer($file) {
    $index = array();
    $find = array('/\r/','/\n/','/\t/','!',',','.','"',';',                           ':');
    $replace = array(' ',' ',' ',' ',' ',' ',' ',' ',' ');
    $string = file_get_contents($file);
    $string = strip_tags($string);
    $string = strtolower($string);
    $string = str_replace($find, $replace, $string);
    $string = trim($string);
    $string = explode(' ', $string);
    natcasesort($string);
    $i = 0;
    foreach($string as $word) {
        $word = trim($word);
        $ignore = preg_match('/[^a-zA-Z]/', $word);
        if($ignore == 1) {
            $word = '';
        }
        if( (!empty($word)) && ($word != '') ) {
            if(!isset($index[$i]['word'])) {
                $index[$i]['word'] = $word;
                $index[$i]['count'] = 1;
            } elseif( $index[$i]['word'] == $word ) {
                $index[$i]['count'] += 1;
            } else {
                $i++;
                $index[$i]['word'] = $word;
                $index[$i]['count'] = 1;
            }
        }
    }
    unset($work);
    return($index);
}

我首先需要弄清楚如何在目录中找到所有htm或html文件,然后在每个htm / html文件上开始使用上面的代码。任何帮助将不胜感激,谢谢!

5 个答案:

答案 0 :(得分:2)

好吧,因为这是家庭作业,我不会给你代码。但我可以指出你正确的方向。通常对于这种类型的事物,人们使用递归函数。函数调用自身的地方。

此功能应执行以下操作:

  • 计算当前目录中所有htm和html文件的所有行。
  • 添加这些数字,然后将它们添加到函数外部的全局变量中(只需使用全局,您可以返回每次调用的行数,然后将它们相加,但这是一个痛苦的对接)
  • 再次为当前目录中的每个文件夹调用此函数(只需循环遍历它们)
  • 一旦回到最开始,重置全局变量,并返回其值

答案 1 :(得分:1)

RecursiveDirectoryIterator是PHP中用来做这个的最好的类。它灵活而快速。

Directory to array with PHP”中描述了其他替代方法(非递归)。在我对这个问题的回答中,我计算了其他答案给出的不同方法,但PHP代码中的所有解决方案都比使用PHP的SPL类慢。

答案 2 :(得分:1)

尝试使用glob功能。

$files = glob('*.htm*');
foreach($files as $file) {
//code here
}

编辑:

    function readDir($path) {
  $files = glob($path . '*.*');

  foreach ($files as $file) {
    if (is_dir($file)) {
      $html_files = array_merge((array) readDir($file . '/'), (array) $html_files);
    }

    if (in_array(strtolower(end(explode('.', $file))), array('html', 'htm'))) {
      $html_files[] = $file;
    }
  }

  return $html_files;
}

刚刚编辑了答案,试试这个。 (注意:我没有在任何网站上测试过代码。) 感谢

答案 3 :(得分:1)

以下是使用RecursiveIteratorIteratorRecursiveDirectoryIteratorpathinfo()的替代方案。

<?php

$dir = '/';

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);

foreach ( $iterator as $path )
  if ( $path->isFile() && preg_match('/^html?$/i', pathinfo($path->getFilename(), PATHINFO_EXTENSION)) )
    echo $path->getPathname() . PHP_EOL;

如果您需要获取当前工作目录,可以使用getcwd()(即$dir = getcwd();)。

要获得内容的长度,您可以做一些事情。您可以使用file_get_contents检索文件的内容,并使用strlen计算长度或str_word_count来计算单词。另一种选择可能是使用$path->getSize()

如果使用数组来存储名称和大小,则可以使用自定义函数和uasort按大小对数组进行排序。

更完整的例子:

<?php

function sort_by_size($a, $b)
{
  if ( $a['size'] == $b['size'] )
    return 0;

  return ( $a['size'] < $b['size'] ? -1 : 1 );
}

$dir = '/';
$files = array();

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);

foreach ( $iterator as $path )
  if ( $path->isFile() && preg_match('/^html?$/i', pathinfo($path->getFilename(), PATHINFO_EXTENSION)) )
    $files[] = array(
      'name' => $path->getPathname(),
      'size' => $path->getSize()
    );

uasort($files, sort_by_size);

然后可以使用$files循环循环foreach数组。它将包含路径名和大小。

答案 4 :(得分:0)

您对可以使用的功能/类有任何限制吗?如果没有,那么检查RecursiveDirectoryIterator它将让你通过dirs递归迭代目录中的所有项目。然后,您可以匹配每个项目的扩展名,如果匹配,则基本上进行计数。

另一种方法是在迭代目录时使用glob,这样您就可以像使用* nix实用程序*.html一样进行find搜索。

就计算而言,您可能需要查看str_word_count