PHP方法获取文件中的行数并将文件指针移动到指定行


PHP way to get count of lines in file and to move file pointer to specified line?

有没有办法:

  1. 计算我的small_db.php中的行数(1 行 = 数据行)
  2. 告诉 PHP 将文件指针精确移动到第 322 行的开头。(所以我可以从这一行获得 fgets() 数据)
  3. 告诉 PHP 返回出现文本"快速棕色狐狸"的行号或整个内容?

我知道还有另一种方法可以将文件的所有内容放在一个字符串中,但是如您所知,如果文件大小很大,该方法的时间和内存效率低下。

首先,这里有一些基本变量:

$filename = 'small_db.php';
$linenum  = 322;
$needle = 'Quick brown fox';
  1. 如何计算文件中的行数?

    若要快速轻松地执行此操作,请在打开文件后使用 count() file()

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

    但是,如果您的文件很大,这将使用大量内存,因为它必须将整个文件加载到内存中才能计算 file() 的返回值。或者,您可以使用标准文件函数 fopen()feof()fgets()fclose() 打开文件并逐行读取,并随时保持计数,如下所示:

    $count = 0;
    $fp = fopen( $filename, 'r');
    while( !feof( $fp)) {
        fgets( $fp);
        $count++;
    }
    fclose( $fp);
    
  2. 如何将文件指针移动到特定行号的开头?

    最好使用 SplFileObject 类来实现这一点。使用要打开的文件的文件名创建此类的新对象,然后使用 seek() 查找所选的行号。然后,您可以使用 key() 显示行号和current()(或getCurrentLine()fgets())以获取该行的内容,如下所示:

    // Create a new object for the file
    $file = new SplFileObject( $filename);
    // Seek to to the specific line number
    $file->seek( $linenum);  
    // Print that line:
    echo 'Line #' . $file->key() . ' contains: ' . $file->current();
    
  3. 如何返回出现特定针头的行号或行的全部内容?

    据我所知,没有内置的PHP函数/方法可以做到这一点。您需要使用与此函数类似的功能自行解析它,该函数使用 strpos() 检查文件中的每一行,由其文件指针 $fp 根据特定的区分大小写的$needle(您可以使用 stripos() 进行不区分大小写的搜索):

    function find_in_file( $fp, $needle) {
        rewind( $fp); // Or: fseek($fp, 0);
        $line_number = 0;
        while( !feof( $fp)) {
            $line = fgets( $fp);
            if( !( strpos( $line, $needle) === false)) {
                break;
            }
            $line_number++;
        }
        return feof( $fp) ? null : array( 
            'line_number' => $line_number,
            'line' => fgets( $fp)
        );
    }
    

    您需要像这样调用此函数:

    $fp = fopen( $filename, 'r');
    $return = find_in_file( $fp, $needle);
    if( !is_null( $return)) {
        echo 'Found ' . $needle . ' in line #' . $return['line_number'] . "'n";
        echo 'That line contains: ' . $return['line'];
    }
    fclose( $fp);