Hướng dẫn write a php script to count number of lines in a file - viết một tập lệnh php để đếm số dòng trong một tệp

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận This file is used for testing all the following PHP codes

    Geeks
    For
    Geeks

    Đưa ra một tham chiếu tệp, tìm số lượng dòng trong tệp này bằng PHP. Có tổng cộng 3 cách tiếp cận để giải quyết điều này. Load the whole file into memory and then use the count() function to return the number of lines in this file.

    Example:

    test.txt: tệp này được sử dụng để kiểm tra tất cả các mã PHP sau

    Cách tiếp cận 1: Tải toàn bộ tệp vào bộ nhớ và sau đó sử dụng hàm Count () để trả về số lượng dòng trong tệp này.

    PHP

        $filePath

    3
    0
    3
    1
    3
    2

    3
    4

    Output:

    3

        

    3
    4
    3
    0
    3
    6
    3
    7$filePath
    3
    9

        

    3
    1
    3
    4
    3
    2
    Here we will load only one line of files at a time which is better than the1st approach. PHP fopen() function is used to open a file or URL. The PHP fgets()function is used to return a line from an open file. Thefclose() function is used to close the file pointer.

    Example:

    test.txt: tệp này được sử dụng để kiểm tra tất cả các mã PHP sau

        $filePath

    3
    8
    3
    1
    3
    2

    Cách tiếp cận 1: Tải toàn bộ tệp vào bộ nhớ và sau đó sử dụng hàm Count () để trả về số lượng dòng trong tệp này.

    PHP

        

    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0;
    
        while (!feof($f)) {
            $lines += substr_count(fread($f, 8192), "\n");
        }
    
        fclose($f);
    
        return $lines;
    }
    
    4
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0;
    
        while (!feof($f)) {
            $lines += substr_count(fread($f, 8192), "\n");
        }
    
        fclose($f);
    
        return $lines;
    }
    
    5
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0;
    
        while (!feof($f)) {
            $lines += substr_count(fread($f, 8192), "\n");
        }
    
        fclose($f);
    
        return $lines;
    }
    
    6
    3
    8
    3
    5
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0;
    
        while (!feof($f)) {
            $lines += substr_count(fread($f, 8192), "\n");
        }
    
        fclose($f);
    
        return $lines;
    }
    
    9

        $filePath

    3
    0
    3
    1
    3
    2

                 +-------------+------------------+---------+
                 | This answer | Dominic's answer | wc -l   |
    +------------+-------------+------------------+---------+
    | Lines      | 3550388     | 3550389          | 3550388 |
    +------------+-------------+------------------+---------+
    | Runtime    | 1.055       | 4.297            | 0.587   |
    +------------+-------------+------------------+---------+
    
    0
    3
    2
                 +-------------+------------------+---------+
                 | This answer | Dominic's answer | wc -l   |
    +------------+-------------+------------------+---------+
    | Lines      | 3550388     | 3550389          | 3550388 |
    +------------+-------------+------------------+---------+
    | Runtime    | 1.055       | 4.297            | 0.587   |
    +------------+-------------+------------------+---------+
    
    9

        

    
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0; $buffer = '';
    
        while (!feof($f)) {
            $buffer = fread($f, 8192);
            $lines += substr_count($buffer, "\n");
        }
    
        fclose($f);
    
        if (strlen($buffer) > 0 && $buffer[-1] != "\n") {
            ++$lines;
        }
        return $lines;
    }
    
    
    1

        

    
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0; $buffer = '';
    
        while (!feof($f)) {
            $buffer = fread($f, 8192);
            $lines += substr_count($buffer, "\n");
        }
    
        fclose($f);
    
        if (strlen($buffer) > 0 && $buffer[-1] != "\n") {
            ++$lines;
        }
        return $lines;
    }
    
    
    3
    3
    5
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0;
    
        while (!feof($f)) {
            $lines += substr_count(fread($f, 8192), "\n");
        }
    
        fclose($f);
    
        return $lines;
    }
    
    2

        

    3
    4
    3
    0
    3
    6
    3
    7$filePath
    3
    9

    3
    4

    Output:

    3

        

    3
    1
    3
    4
    3
    2

    Cách tiếp cận này tải toàn bộ tệp vào bộ nhớ có thể gây ra sự cố tràn bộ nhớ.We will load only a specific size of data into memory. We don’t care about line breaks at the time of loading. We will iterate on the loaded data to count the number of line breaks in it.

    Example:

    test.txt: tệp này được sử dụng để kiểm tra tất cả các mã PHP sau

        $filePath

    3
    8
    3
    1
    3
    2

    Cách tiếp cận 1: Tải toàn bộ tệp vào bộ nhớ và sau đó sử dụng hàm Count () để trả về số lượng dòng trong tệp này.

    PHP

        

    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0;
    
        while (!feof($f)) {
            $lines += substr_count(fread($f, 8192), "\n");
        }
    
        fclose($f);
    
        return $lines;
    }
    
    4
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0;
    
        while (!feof($f)) {
            $lines += substr_count(fread($f, 8192), "\n");
        }
    
        fclose($f);
    
        return $lines;
    }
    
    5
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0;
    
        while (!feof($f)) {
            $lines += substr_count(fread($f, 8192), "\n");
        }
    
        fclose($f);
    
        return $lines;
    }
    
    6
    3
    8
    3
    5
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0;
    
        while (!feof($f)) {
            $lines += substr_count(fread($f, 8192), "\n");
        }
    
        fclose($f);
    
        return $lines;
    }
    
    9

        $filePath

    3
    0
    3
    1
    3
    2

        

    3
    4
    3
    0
    3
    6
    3
    7$filePath
    3
    9

        

    
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0; $buffer = '';
    
        while (!feof($f)) {
            $buffer = fread($f, 8192);
            $lines += substr_count($buffer, "\n");
        }
    
        fclose($f);
    
        if (strlen($buffer) > 0 && $buffer[-1] != "\n") {
            ++$lines;
        }
        return $lines;
    }
    
    
    1

        

    
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0; $buffer = '';
    
        while (!feof($f)) {
            $buffer = fread($f, 8192);
            $lines += substr_count($buffer, "\n");
        }
    
        fclose($f);
    
        if (strlen($buffer) > 0 && $buffer[-1] != "\n") {
            ++$lines;
        }
        return $lines;
    }
    
    
    3
    3
    5
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0;
    
        while (!feof($f)) {
            $lines += substr_count(fread($f, 8192), "\n");
        }
    
        fclose($f);
    
        return $lines;
    }
    
    2

        

    3
    4
    3
    0
    3
    6
    3
    7$filePath
    3
    9

        

    3
    1
    3
    4
    3
    2

    Output:

    3

    Cách tiếp cận này tải toàn bộ tệp vào bộ nhớ có thể gây ra sự cố tràn bộ nhớ.


    Sử dụng vòng lặp của các cuộc gọi

    3
    21 là giải pháp tốt và đơn giản nhất để viết, tuy nhiên:

    1. Mặc dù bên trong tệp được đọc bằng bộ đệm 8192 byte, mã của bạn vẫn phải gọi hàm đó cho mỗi dòng.

    2. Về mặt kỹ thuật, một dòng có thể lớn hơn bộ nhớ có sẵn nếu bạn đang đọc tệp nhị phân.

    Mã này đọc một tệp trong các khối 8kb mỗi lần và sau đó đếm số lượng dòng mới trong khối đó.

    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0;
    
        while (!feof($f)) {
            $lines += substr_count(fread($f, 8192), "\n");
        }
    
        fclose($f);
    
        return $lines;
    }
    

    Nếu độ dài trung bình của mỗi dòng là tối đa 4kb, bạn sẽ bắt đầu lưu vào các cuộc gọi chức năng và chúng có thể thêm vào khi bạn xử lý các tệp lớn.

    Điểm chuẩn

    Tôi đã chạy một bài kiểm tra với một tệp 1GB; Đây là kết quả:

                 +-------------+------------------+---------+
                 | This answer | Dominic's answer | wc -l   |
    +------------+-------------+------------------+---------+
    | Lines      | 3550388     | 3550389          | 3550388 |
    +------------+-------------+------------------+---------+
    | Runtime    | 1.055       | 4.297            | 0.587   |
    +------------+-------------+------------------+---------+
    

    Thời gian được đo bằng giây thời gian thực, xem ở đây

    Số lượng dòng thực sự

    Mặc dù ở trên hoạt động tốt và trả về các kết quả tương tự như

    3
    22, nếu tệp kết thúc mà không có dòng mới, số dòng sẽ bị tắt bởi một; Nếu bạn quan tâm đến kịch bản cụ thể này, bạn có thể làm cho nó chính xác hơn bằng cách sử dụng logic này:

    
    function getLines($file)
    {
        $f = fopen($file, 'rb');
        $lines = 0; $buffer = '';
    
        while (!feof($f)) {
            $buffer = fread($f, 8192);
            $lines += substr_count($buffer, "\n");
        }
    
        fclose($f);
    
        if (strlen($buffer) > 0 && $buffer[-1] != "\n") {
            ++$lines;
        }
        return $lines;
    }
    
    

    Làm cách nào để đếm số lượng dòng trong tệp PHP?

    Cách tiếp cận 1: Tải toàn bộ tệp vào bộ nhớ và sau đó sử dụng hàm Count () để trả về số lượng dòng trong tệp này.Load the whole file into memory and then use the count() function to return the number of lines in this file.

    Làm cách nào để đếm số lượng từ trong một chuỗi trong PHP?

    Cách tiếp cận 1: Sử dụng phương thức str_word_count (): Phương thức str_word_count () được sử dụng để đếm số lượng từ trong một chuỗi.Using str_word_count() Method: The str_word_count() method is used to counts the number of words in a string.