我有一个csv文件"acct-2012-08-24-0001.csv",其中包含许多行。我想数一下行中包含单词"118bonsplays.com"的所有行。但我不知道如何计数,我只能像下面的php代码一样计数csv文件中的所有行:
$word = "118-bonsplans.com";
$linecount = count(file('acct-2012-08-24-0001.csv'));
echo $linecount;
我不知道该怎么继续。有人帮我吗,谢谢,
怎么样:
count(preg_grep("/118'-bonsplans'.com/", file("acct-2012-08-24-0001.csv")));
preg_grep的第一个参数是正则表达式模式。第二个是要搜索的数组。它返回搜索数组中匹配的项的数组。
您可以尝试使用以下代码:
$handle = fopen('acct-2012-08-24-0001.csv', 'r');
$counter = 0;
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
if (stristr($buffer, '118-bonsplans.com')) $counter += 1;
}
fclose($handle);
}
echo $counter;