【发布时间】:2011-09-23 05:55:13
【问题描述】:
对于家庭作业,我必须获取当前目录和所有子目录中的所有 .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 文件上使用上述代码。任何帮助将不胜感激,谢谢!
【问题讨论】:
-
使用 glob() 从目录中获取所有文件.. phpcodebase.com/php-magic-function-glob
标签: php html directory indexing