计算包含特定字符串的文件数[PHP]
我有一个包含子目录的目录,我试图循环所有子目录,并循环文件名,以计算其中有字符串的次数 子目录内部:(我得到这个输出) ../conva/96376/96376-200-2019-03-28-16-15-49.wav ../conva/96376/96376-200-2019-04-01-11-46-52.wav ../conva/96376/96376-200-2019-04-01-11-47-27.wav ../conva/96376/96376-263-2020-01-06-09-40-24.wav ../conva/96376/96376-263-2020-01-06-10-08-16.wav 在这里,我需要计算有多少文件具有(200)或(263)才能生成报告 我已经隔离了我需要的号码,并将其存储在一个变量($poste)中。我只是不知道我在这里该怎么算,因为我没有什么可比的。这是我到目前为止得到的 200和263不是硬编码的(我永远不会事先知道数字),可能还有更多的数字需要计算计算包含特定字符串的文件数[PHP],php,Php,我有一个包含子目录的目录,我试图循环所有子目录,并循环文件名,以计算其中有字符串的次数 子目录内部:(我得到这个输出) ../conva/96376/96376-200-2019-03-28-16-15-49.wav ../conva/96376/96376-200-2019-04-01-11-46-52.wav ../conva/96376/96376-200-2019-04-01-11-47-27.wav ../conva/96376/96376-263-2020-01-06-09-40-2
$directory = '../convo/';
$count = 0;
$subDirectories = scandir($directory);
unset($subDirectories[0]);
unset($subDirectories[1]);
foreach ($subDirectories as $subDirectory) {
echo '<h2>' . $subDirectory . '</h2>';
foreach (glob($directory . $subDirectory . '/*') as $file) {
$poste_explode = explode('-', $file);
$poste = $poste_explode[1];
}
}
$directory='../conva/';
$count=0;
$subDirectories=scandir($directory);
未设置($subDirectories[0]);
未设置($子目录[1]);
foreach($子目录作为$子目录){
回显“.$subDirectory.”;
foreach(glob($directory.$subDirectory.'/*')作为$file){
$poste_explode=explode('-',$file);
$poste=$poste_爆炸[1];
}
}
如果您只需要200和263的计数,请尝试以下方法:
$directory = '../convo/';
$count = 0;
$subDirectories = scandir($directory);
unset($subDirectories[0]);
unset($subDirectories[1]);
foreach ($subDirectories as $subDirectory) {
echo '<h2>' . $subDirectory . '</h2>';
foreach (glob($directory . $subDirectory . '/*') as $file) {
$poste_explode = explode('-', $file);
$poste = $poste_explode[1];
if (in_array($poste,["200","263"])) {
$count++;
}
}
}
$directory='../conva/';
$count=0;
$subDirectories=scandir($directory);
未设置($subDirectories[0]);
未设置($子目录[1]);
foreach($子目录作为$子目录){
回显“.$subDirectory.”;
foreach(glob($directory.$subDirectory.'/*')作为$file){
$poste_explode=explode('-',$file);
$poste=$poste_爆炸[1];
if(在数组中($poste,[“200”,“263”])){
$count++;
}
}
}
如果您只需要200和263的计数,请尝试以下方法:
$directory = '../convo/';
$count = 0;
$subDirectories = scandir($directory);
unset($subDirectories[0]);
unset($subDirectories[1]);
foreach ($subDirectories as $subDirectory) {
echo '<h2>' . $subDirectory . '</h2>';
foreach (glob($directory . $subDirectory . '/*') as $file) {
$poste_explode = explode('-', $file);
$poste = $poste_explode[1];
if (in_array($poste,["200","263"])) {
$count++;
}
}
}
$directory='../conva/';
$count=0;
$subDirectories=scandir($directory);
未设置($subDirectories[0]);
未设置($子目录[1]);
foreach($子目录作为$子目录){
回显“.$subDirectory.”;
foreach(glob($directory.$subDirectory.'/*')作为$file){
$poste_explode=explode('-',$file);
$poste=$poste_爆炸[1];
if(在数组中($poste,[“200”,“263”])){
$count++;
}
}
}
只需为值添加一个测试,并在此处放置一个计数器:
$posteCounter = 0;
foreach (glob($directory . $subDirectory . '/*') as $file) {
$poste_explode = explode('-', $file);
$poste = $poste_explode[1];
if(200 == $poste || 263 == $poste) {
$posteCounter++;
}
或者,您可以对每一个进行计数,这样您就可以得到单独的值,这些值可以相加:
//set variables outside the loop to hold the values
$_200 = 0;
$_263 = 0;
if(200 == $poste) {
$_200++;
} elseif (263 == $poste) {
$_263++;
}
根据OP提供的评论编辑:
为了找到所有poste值,然后进行计数,您必须在目录中循环两次。第一个循环将构建一个数组:
foreach(glob($directory . $subDirectory . '/*') as $file) {
$poste_explode = explode('-', $file);
$postes[] = $poste_explode[1]; // create and populate $postes array
}
现在,您可以在$postes
数组中循环并使用这些值计算文件,为每个poste数字设置一个变量(可能有更好的方法,但咖啡的缺乏并不会让人想起它们)。这是一个
只需为这些值添加一个测试,并在此处放置一个计数器:
$posteCounter = 0;
foreach (glob($directory . $subDirectory . '/*') as $file) {
$poste_explode = explode('-', $file);
$poste = $poste_explode[1];
if(200 == $poste || 263 == $poste) {
$posteCounter++;
}
或者,您可以对每一个进行计数,这样您就可以得到单独的值,这些值可以相加:
//set variables outside the loop to hold the values
$_200 = 0;
$_263 = 0;
if(200 == $poste) {
$_200++;
} elseif (263 == $poste) {
$_263++;
}
根据OP提供的评论编辑:
为了找到所有poste值,然后进行计数,您必须在目录中循环两次。第一个循环将构建一个数组:
foreach(glob($directory . $subDirectory . '/*') as $file) {
$poste_explode = explode('-', $file);
$postes[] = $poste_explode[1]; // create and populate $postes array
}
现在,您可以在$postes
数组中循环并使用这些值计算文件,为每个poste数字设置一个变量(可能有更好的方法,但咖啡的缺乏并不会让人想起它们)。这是一个
我唯一的问题是,我不知道我必须设置多少变量,每次都会改变。你没有在最初的问题中声明,这将需要一组完全不同的逻辑,可能需要一个变量数组来实现。是的,对于每个目录,你扫描检查一个poste的数组。如果poste不在那里,它将被添加到数组中。然后您可以检查poste编号并将其递增。逻辑是完全不同的,因为你会首先构建数组,通过数组循环找到每个文件,然后用poste来累积帐户。我做到了,它成功了,非常感谢@Jay是一套更为完整的代码,它会在对文件名进行计数后将其从数组中删除,以避免对文件进行过多计数。我唯一的问题是,我不知道要设置多少变量,每次都会更改。您在原始版本中没有声明,这将需要一组完全不同的逻辑,可能需要一个变量数组来实现。是的,对于每个扫描的目录,检查一个poste的数组。如果poste不在那里,它将被添加到数组中。然后您可以检查poste编号并将其递增。逻辑是完全不同的,因为你会首先构建数组,通过数组循环找到每个文件,然后用poste来累积帐户。我做到了,它成功了,非常感谢@Jay是一组更为完整的代码,它在对文件名进行计数后从数组中删除文件名,以避免对文件进行过多计数。