PHP如何获取所有子目录中的所有文件(仅html文件)并为每个html页面编制索引
对于家庭作业,我必须获取当前目录和所有子目录中的所有.htm和.html文件,并通过分别计算文件中出现的所有单词来为它们编制索引 以下是我在目录中找到html文件后如何计算文件数:PHP如何获取所有子目录中的所有文件(仅html文件)并为每个html页面编制索引,php,html,directory,indexing,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 =
$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);
}
$file='.html';
$index=索引器($file);
回显“”。打印($index,true)。“”;
函数索引器($file){
$index=array();
$find=array(“/\r/”、“/\n/”、“/\t/”、“!”、“、”、“、”、“;”、“:”);
$replace=数组(“”、“”、“”、“”、“”、“”、“”、“”、“”、“”);
$string=file\u get\u contents($file);
$string=带标签($string);
$string=strtolower($string);
$string=str_replace($find,$replace,$string);
$string=修剪($string);
$string=分解(“”,$string);
natcasesort($string);
$i=0;
foreach($word形式的字符串){
$word=trim($word);
$ignore=preg_match('/[^a-zA-Z]/',$word);
如果($ignore==1){
$word='';
}
如果((!empty($word))&($word!=''){
如果(!isset($index[$i]['word'])){
$index[$i]['word']=$word;
$index[$i]['count']=1;
}elseif($index[$i]['word']==$word){
$index[$i]['count']+=1;
}否则{
$i++;
$index[$i]['word']=$word;
$index[$i]['count']=1;
}
}
}
未结算(工作);
回报(指数);
}
我只需要先弄清楚如何找到目录中的所有htm或html文件,然后开始在每个htm/html文件上使用上述代码。任何帮助都将不胜感激,谢谢!好吧,因为这是一个家庭作业,我不会给你代码。但我可以为你指出正确的方向。通常对于这类事情,people使用递归函数。函数调用自身 此功能应执行以下操作:
- 计算当前目录中所有htm和html文件的所有行数
- 将这些数字相加,然后将它们添加到函数外部的全局变量中(只需使用global,您可以返回每次调用的行数,并将它们相加,但这是一个棘手的问题)
- 对当前目录中的每个文件夹再次调用此函数(只需循环遍历它们)
- 回到起点后,重置全局变量并返回其值
- 计算当前目录中所有htm和html文件的所有行数
- 将这些数字相加,然后将它们添加到函数外部的全局变量中(只需使用global,您可以返回每次调用的行数,并将它们相加,但这是一个棘手的问题)
- 对当前目录中的每个文件夹再次调用此函数(只需循环遍历它们)
- 回到起点后,重置全局变量并返回其值
- 好吧,因为这是一个家庭作业,我不会给你代码。但我可以为你指出正确的方向。通常对于这类事情,人们使用递归函数。函数调用自身
此功能应执行以下操作:
- 是PHP中实现这一点的最佳类。它灵活且快速
在“”中描述了其他替代方法(不是递归的)。在我对该问题的回答中,我对其他答案给出的不同方法进行了计时,但PHP代码中的所有解决方案都比使用PHP的SPL类慢。是PHP中实现这一点的最佳类。它灵活且快速
其他替代方法(非递归)如所述““。在我对该问题的回答中,我对其他答案给出的不同方法进行了计时,但PHP代码中的所有解决方案都比使用PHP的SPL类慢。您对可以使用的函数/类有任何限制吗?如果没有,那么检查它将让您递归地遍历目录中的所有项。然后,您可以匹配每个项目的扩展名,如果它基本上匹配,则进行计数 另一种方法是在遍历目录时使用,它允许您执行
*.html
搜索,就像使用*nix实用程序查找
一样
关于计数,您可能想看看。您对可以使用的函数/类有任何限制吗?如果没有,那么检查它将让您递归地遍历目录中的所有项。然后,您可以匹配每个项目的扩展名,如果它基本上匹配,则进行计数 另一种方法是在遍历目录时使用,它允许您执行
*.html
搜索,就像使用*nix实用程序查找
一样
至于计数,您可能需要查看。尝试使用函数
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;
}
编辑:
<?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;
刚刚编辑了答案,试试这个。(注意:我没有在任何网站上测试过代码。)
谢谢尝试使用该功能
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;
}
编辑:
<?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;
刚刚编辑了答案,试试这个。(注意:我没有在任何网站上测试过代码。)
谢谢这里有一个使用和的替代方法
然后可以使用
foreach
循环来循环$files
数组。它将同时包含路径名和大小 这里有一个替代方法,使用和
然后可以使用
foreach
循环来循环$files
数组。它将同时包含路径名和大小 使用glob()从目录中获取所有文件。。使用glob()从目录中获取所有文件。。这不会递归地工作。这个问题询问当前目录和所有子目录。我必须首先在当前目录和子目录中搜索所有.htm文件,然后