对于家庭作业,我必须在当前和所有子目录中获取所有.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文件上开始使用上面的代码。任何帮助将不胜感激,谢谢!
答案 0 :(得分:2)
好吧,因为这是家庭作业,我不会给你代码。但我可以指出你正确的方向。通常对于这种类型的事物,人们使用递归函数。函数调用自身的地方。
此功能应执行以下操作:
答案 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)
以下是使用RecursiveIteratorIterator
,RecursiveDirectoryIterator
和pathinfo()
的替代方案。
<?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
。