【问题标题】:Count lines of file in PHP on Windows在 Windows 上计算 PHP 中的文件行数
【发布时间】:2012-06-23 11:25:25
【问题描述】:

要确定我当前使用的文件中的确切行数:

if(exec("wc -l ".escapeshellarg($strFile), $arResult)) {
     $arNum = explode(" ", $arResult[0]);
     // ...
  }

在 Windows 上执行相同操作的最佳方法是什么?


编辑:

另一个问题的尝试:

$file="largefile.txt";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
  $line = fgets($handle);
  $linecount++;
}

fclose($handle);

echo $linecount;
  1. 有没有人用这种方式使用大文件的经验?

  2. 除了 PHP 函数之外,有没有使用 Windows 命令 来确定文件大小的方法?


解决方案

我按照 cmets 中接受的答案的建议使用命令 find。

【问题讨论】:

  • 目前所有答案都推荐file()。考虑到这个函数会将整个文件加载到内存中!
  • 可能是 related 来计算文件中的行数
  • @tradyblix - 不错的收获。我确定这个问题是骗人的,但我找不到它:)
  • 感谢您的回答。我在您的链接中找到了答案,请检查我最初发布的编辑。
  • 正如代码来自的答案中所述,您可以找到非常大的行(例如二进制文件)。使用第二个 sn-p。

标签: php windows numbers exec lines


【解决方案1】:

也许你可以使用:

$length = count(file($filename));

这在任何地方都可以使用。

file() 将文件读入一个数组,以换行符分割,count() 计算数组的长度。

如果它不能正常工作(例如在 macintosh 文件中),请看这里:http://www.php.net/manual/en/filesystem.configuration.php#ini.auto-detect-line-endings

【讨论】:

  • 应该可以,但请注意它将文件加载到内存中,这对大文件不利。
【解决方案2】:

我更喜欢只循环文件,每次读取一行并增加一个计数器,使用和计算file() 返回的数组只适用于较小的文件。

<?php

$loc = 'Ubuntu - 10.10 i386.iso';

$f = fopen($loc,'r');
$count = 0;

while (fgets($f)) $count++;

fclose($f);

print "Our file has $count lines" . PHP_EOL;

如果您将file() 用于如此大的文件,它会将其完全读入内存,根据您的情况,这可能会令人望而却步。如果这是 1 次“我不在乎,这是我的工作站,我有足够的内存”的情况,或者文件保证很小,那么您可以使用

count(file($loc));

否则我会循环执行,尤其是因为 if 操作必须由许多进程执行。两种计数方式都会遍历整个文件,但在第二种情况下内存会大大增加。

【讨论】:

  • 你意识到你可以通过 cygwin 继续使用 wc -l 对吗? :)
  • 我无法控制服务器环境。
  • 你能通过 shell_exec/exec 运行一个批处理脚本 /DOS 命令吗?像find /V /C "&amp;!@Not_Existing&amp;!@" my_file.text 这样的字符串在你的文件中的机会应该很小:)
  • 谢谢!我的代码:if(exec('find /V /C "&amp;!@Not_Existing&amp;!@" '. escapeshellarg($strFile), $arResult)) { $arResult[0]; } else { // ... try fopen() } 我不知道find 返回什么!行号是$arResult 还是$arResult[0]?这里没有窗户。
  • 我也不是,但它似乎是第二个参数(robvanderwoude.com/find.php)只需执行 print_r 并找出答案。 :)
【解决方案3】:

计算行号的Windows命令:

find /c /v "" < type file-name.txt

改编自Stupid command-line trick: Counting the number of lines in stdin。

【讨论】:

    【解决方案4】:

    这是使用substr_count,比fgets快得多:

    $file="largefile.txt";
    $linecount = 0;
    $chunk_size = (2<<20); // 2MB chuncks
    
    $handle = fopen($file, "r");
    
    while(!feof($handle)){
        $chunk = fread($handle,$chunk_size);
        $linecount += substr_count($chunk,PHP_EOL);
        // $linecount += substr_count($chunk,"\n"); // also with \n, \r, or \r\n
    }
    fclose($handle);
    echo $linecount;
    

    代码考虑了使用最少内存(2 MB 块)。 Benchmark 具有 85 MB 文件和 8M+ 行,执行时间为:
    • fgets:52.11271 秒。
    • substr_count(PHP_EOL):0.58844 秒。
    • substr_count(\n):0.353772 秒。
    • find /c /v "" largefile.txt:100 秒。

    但是,如果主机系统上的可用内存没有问题,比如 OP,并且在 PHP 中设置了适当的内存限制(大于文件长度),substr_count 可以搜索文件的全部内容很多表现:

    $file="largefile.txt";
    @ini_set('memory_limit', (2<<24)+(filesize($file)) ); // 32 MB for PHP + File size
    $linecount = 0;
    $handle = file_get_contents($file);
    if($handle) $linecount = substr_count($handle, PHP_EOL);
    echo $linecount;
    

    您可以为解释器选择所需的任何内存大小。
    基准测试:0.46878 秒。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 2018-01-27
      • 2016-08-26
      • 2012-04-27
      相关资源
      最近更新 更多