【问题标题】:Display count of files/items that match specific conditions before listing those items在列出这些项目之前显示符合特定条件的文件/项目的计数
【发布时间】:2019-12-06 14:37:07
【问题描述】:

我似乎无法在任何地方找到答案。我正在尝试计算 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')); 只是为了测试一个条件,但这没有产生任何结果。解决这个问题的正确方法是什么?

【问题讨论】:

  • 除了下面给出的答案,你可以简单地从字符串中正则表达式你想要的东西,而不是一块一块地剥离它
  • 我一直在尝试找出 regex / preg_match 以使其能够满足我的需求,但是我觉得我会回到它。就目前而言,剥离它似乎至少能让我得到我想要的结果,这样我就可以继续前进,当我对它不感到沮丧时再回到它。哈哈。文件名包括:(1)“商店评估_”或“商店调查_”,(2) 4 位或 5 位商店编号,(3) 年月日,(4) 4 位提交代码。示例:“商店评估_10950_2019-12-03_6980.pdf”。我需要输出:“10950(2019-12-03)”

标签: php if-statement count conditional-statements


【解决方案1】:

您可以将其存储在一个数组中,然后在$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

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 2015-02-04
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    相关资源
    最近更新 更多