Php 在列出符合特定条件的文件/项目之前,显示这些文件/项目的计数

Php 在列出符合特定条件的文件/项目之前,显示这些文件/项目的计数,php,if-statement,count,conditional-statements,Php,If Statement,Count,Conditional Statements,我似乎到处都找不到答案。我试图计算FTP文件夹中满足特定条件的文件数。我正在使用以下工具: /* * Filename examples: * Store Evaluation_10950_2019-12-03_6980.pdf * Store Survey_13532_2019-11-29.pdf */ $file_list = ftp_nlist($ftp_connection, "."); $currentDate = date('Y-m'); $countFiles =

我似乎到处都找不到答案。我试图计算FTP文件夹中满足特定条件的文件数。我正在使用以下工具:

/*
 * Filename examples: 
 * Store Evaluation_10950_2019-12-03_6980.pdf
 * Store Survey_13532_2019-11-29.pdf
 */

$file_list = ftp_nlist($ftp_connection, "."); 
$currentDate = date('Y-m');

$countFiles = count($file_list);

// Title of Completed Evaluatons
echo '<strong>'.$countFiles.' Completed Evaluations:</strong><br><br>'; 

foreach($file_list as $file) {
//Only get Current Month PDF files
    if ((strpos($file, '.pdf') !== false) && (strpos($file, 'Store Evaluation') !== false) && (strpos($file, $currentDate) !== false)) {
        // Remove Store Evaluation_ from string
        $strippedEvals1 = ltrim($eval, 'Store Evaluation_');
        // Remove store number from string
        $strippedEvals2 = strstr($strippedEvals1, '_');
        // Remove _ from string
        $strippedEvals3 = str_replace('_','',$strippedEvals2);
        // Remove everything after the date in string
        $strippedEvalDate = substr_replace($strippedEvals3 ,"", -8); 
        // Get just the store number
        $strippedEvalStoreNum = substr($strippedEvals1, 0, strpos($strippedEvals1, "_")); 
        // Print store number and date
        echo "<strong>".$strippedEvalStoreNum."</strong> (".$strippedEvalDate.")<br>";
    }
}
/*
*文件名示例:
*门店评估_10950_2019-12-03_6980.pdf
*门店调查_13532_2019-11-29.pdf
*/
$file\u list=ftp\u nlist($ftp\u connection,“.”);
$currentDate=日期('Y-m');
$countFiles=count($file\u list);
//已完成评估的标题
回显“”.$countFiles.。已完成的评估:

”; foreach($file\u列表为$file){ //仅获取当月PDF文件 如果((strpos($file,.pdf')!==false)&(strpos($file,'Store Evaluation')!==false)&(strpos($file,$currentDate)!==false)){ //从字符串中删除存储评估 $strippedEvals1=ltrim($eval,'Store Evaluation_'); //从字符串中删除存储编号 $strippedvals2=strstr($strippedvals1,'.'); //从字符串中删除 $strippedEvals3=str_replace(“”,“”,$strippedEvals2); //删除字符串中日期之后的所有内容 $strippedvaldate=substr_replace($strippedvals3,“,-8); //只需要知道商店的号码 $strippedvalstorenum=substr($strippedvals1,0,strpos($strippedvals1,“”); //打印门店号和日期 回显“”$strippedvalStorenum。”(“$strippedvalDate”)
”; } }
我能够根据我指定的条件列出所有文件;然而,现在我想数一数,并说出有多少人在顶端。上面的代码显然输出了文件夹中没有条件的所有文件的数量


我试过
$countFiles=count(strpos($file_list,'.pdf')仅测试单个条件,但这不会产生任何结果。正确的方法是什么?

不必在循环时回显条件,您可以将其存储在数组中,然后在
$output
数组上使用
count()
输出:

$output = array(); //create an empty array to store our output

foreach($file_list as $file) {
//Only get Current Month PDF files
    if ((strpos($file, '.pdf') !== false) && (strpos($file, 'Store Evaluation') !== false) && (strpos($file, $currentDate) !== false)) {
        // Remove Store Evaluation_ from string
        $strippedEvals1 = ltrim($eval, 'Store Evaluation_');
        // Remove store number from string
        $strippedEvals2 = strstr($strippedEvals1, '_');
        // Remove _ from string
        $strippedEvals3 = str_replace('_','',$strippedEvals2);
        // Remove everything after the date in string
        $strippedEvalDate = substr_replace($strippedEvals3 ,"", -8); 
        // Get just the store number
        $strippedEvalStoreNum = substr($strippedEvals1, 0, strpos($strippedEvals1, "_")); 
        // Print store number and date
        $output[] =  "<strong>".$strippedEvalStoreNum."</strong> (".$strippedEvalDate.")"; //add to array instead of echo
    }
}

echo '<strong>'.count($file_list).' Completed Evaluations</strong><br><br>'; 

echo '<strong>'.count($output).' Evaluations Met My Conditions:</strong><br><br>'; //echo the number of files that met the conditions

echo implode('<br/>', $output); //echo the converted file names
$output=array()//创建一个空数组来存储输出
foreach($file\u列表为$file){
//仅获取当月PDF文件
如果((strpos($file,.pdf')!==false)&(strpos($file,'Store Evaluation')!==false)&(strpos($file,$currentDate)!==false)){
//从字符串中删除存储评估
$strippedEvals1=ltrim($eval,'Store Evaluation_');
//从字符串中删除存储编号
$strippedvals2=strstr($strippedvals1,'.');
//从字符串中删除
$strippedEvals3=str_replace(“”,“”,$strippedEvals2);
//删除字符串中日期之后的所有内容
$strippedvaldate=substr_replace($strippedvals3,“,-8);
//只需要知道商店的号码
$strippedvalstorenum=substr($strippedvals1,0,strpos($strippedvals1,“”);
//打印门店号和日期
$output[]=“”$strippedEvalStoreNum。”(“$strippedEvalDate”);//添加到数组而不是echo
}
}
回显“”.count($file_list)。“已完成的评估”;
echo“”.count($output)。“评估符合我的条件:
”//回显满足条件的文件数 回波内爆('
',$output)//回显转换后的文件名
除此之外,下面给出了答案,您可以简单地从字符串中正则化您想要的内容,而不是一块一块地将其剥离。我一直在尝试找出正则化/预匹配,以使其满足我的需要,但经历了如此艰难的一段时间后,我想我会回到它。现在,剥离它似乎至少能让我得到我想要的结果,这样我就可以继续前进,并在不感到沮丧的时候回到它身边。Lol.文件名包括:(1)“门店评估”或“门店调查”;(2)4位或5位门店编号;(3)年-月-日;(4)4位提交代码。示例:“门店评估_10950_2019-12-03_6980.pdf”。我需要输出:“10950(2019-12-03)”