Given the file text.txt, here is its contents:
one
2
3
four
five
6
7
eight

As we see, there are 8 lines in our file. It is necessary for the script to check the contents of the text.txt file and display the text, for example: The text.txt file contains 8 lines. Thank you very much in advance!

  • one
    "As we see this sandwich, please help me identify the sandwich, for example this is a sandwich." For me personally, the question sounds about the same. - Alex Kapustin
  • I hope this helps (read the rest too) softtime.ru/bookphp/gl6_4.php - avp

4 answers 4

$arr=file("file.txt"); echo count($arr); 
     $fileData = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/test.txt'); preg_matсh_all( "/\n/", $fileData, $lines ); echo 'Всего ' . count( $lines ) . ' строк'; =============================================== //ну или как вариант $lines = explode( "\n", file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/test.txt') ); echo 'Всего ' . count( $lines ) . ' строк'; 

    One way or another, I have to initialize the array so that I won’t even say that it will be faster ..

    • Why does "somehow have to initialize the array"? ;) - Alex Kapustin
    • both in the first and in the second case memory is allocated under $lines - Zowie

    oh, kind of got what it takes :)

     $cnt = 0; $f = fopen('path/to/file.txt'); while (!foef($f)) { $cnt ++; fgets($f); } fclose($f); 

    or so. (without initializing the array and loading the file into memory)

      Here, think out the rest yourself:

        $ kolvo = explode ("\ n", $ contents of file);
       $ kolvo = sizeof ($ kolvo);
       echo $ kolvo;  // Number of lines 

      • If you have already crammed the file into memory, then count the number of lines through count (). - 1232