猿问

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

我似乎无法在任何地方找到答案。我正在尝试计算 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>";

    }

}

我能够根据我指定的条件列出所有文件;但是,现在我想数一数并说出顶部有多少。上面的代码显然是在没有条件的情况下输出了文件夹中所有文件的个数。


我试图$countFiles = count(strpos($file_list, '.pdf'));只测试一个条件,但这没有产生任何结果。解决这个问题的正确方法是什么?


三国纷争
浏览 73回答 1
1回答

青春有我

count()不是在循环时回显条件,而是可以将其存储在一个数组中,然后在数组的末尾用 a 输出它$output:$output = array(); //create an empty array to store our outputforeach($file_list as $file) {//Only get Current Month PDF files&nbsp; &nbsp; if ((strpos($file, '.pdf') !== false) && (strpos($file, 'Store Evaluation') !== false) && (strpos($file, $currentDate) !== false)) {&nbsp; &nbsp; &nbsp; &nbsp; // Remove Store Evaluation_ from string&nbsp; &nbsp; &nbsp; &nbsp; $strippedEvals1 = ltrim($eval, 'Store Evaluation_');&nbsp; &nbsp; &nbsp; &nbsp; // Remove store number from string&nbsp; &nbsp; &nbsp; &nbsp; $strippedEvals2 = strstr($strippedEvals1, '_');&nbsp; &nbsp; &nbsp; &nbsp; // Remove _ from string&nbsp; &nbsp; &nbsp; &nbsp; $strippedEvals3 = str_replace('_','',$strippedEvals2);&nbsp; &nbsp; &nbsp; &nbsp; // Remove everything after the date in string&nbsp; &nbsp; &nbsp; &nbsp; $strippedEvalDate = substr_replace($strippedEvals3 ,"", -8);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Get just the store number&nbsp; &nbsp; &nbsp; &nbsp; $strippedEvalStoreNum = substr($strippedEvals1, 0, strpos($strippedEvals1, "_"));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Print store number and date&nbsp; &nbsp; &nbsp; &nbsp; $output[] =&nbsp; "<strong>".$strippedEvalStoreNum."</strong> (".$strippedEvalDate.")"; //add to array instead of echo&nbsp; &nbsp; }}echo '<strong>'.count($file_list).' Completed Evaluations</strong><br><br>';&nbsp;echo '<strong>'.count($output).' Evaluations Met My Conditions:</strong><br><br>'; //echo the number of files that met the conditionsecho implode('<br/>', $output); //echo the converted file names
随时随地看视频慕课网APP
我要回答