繁体   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