簡體   English   中英

php計算目錄和子目錄中文件中的總字符串

[英]php count total string in files in directory and subdirectories

我在目錄和子目錄中有多個文件。 標准文件如下

users.text示例

F: khkgjg1 3hs6q7a5x { expire=2016-08-21 13; afexpire=2017-08-20; email=kgtugh@kkk.com; Country=United State; country_code=US; hosted=none }
F: khkgjg2 hjfg545gh { expire=2016-09-21 13; afexpire=2017-08-20; email=kgtugh@kkk.com; Country=United State; country_code=US; hosted=none }
F: khkgjg3 hgdghgfh5 { expire=2017-08-21 13; afexpire=2017-08-20; email=kgtugh@kkk.com; Country=Duchland; country_code=DE; hosted=none }
F: khkgjg4 y5dhgfdh5 { expire=2015-08-21 13; afexpire=2017-08-20; email=kgtugh@kkk.com; Country=United State; country_code=US; hosted=none }

我有以下代碼,我希望它計算所有txt或cfg文件中具有此字符串“ country_code = US”的總行數。

<?php
$path_to_check  = '/path/phpm/linessysf/test/';
$types_to_check = "{*,ap/*}.{txt,cfg}";
$countrycd = "US";

foreach (glob($path_to_check . $types_to_check, GLOB_BRACE) as $filename) {
    foreach ($rows = file($filename) as $key => $row) {

        if (preg_match("#^F:.*?country_code=($countrycd);#is", $row, $valuesf)) {

        }
    }
}
echo $count_country;

我嘗試這樣做,但是我摔倒了,請問有人可以幫忙嗎?

您可以在preg_match之后簡單地添加計數器增量:

if (preg_match("#\bcountry_code=$countrycd\b;#is", $row)) {
    $count_country++;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM