PHP way to get the number of lines in a file and move the pointer to the specified line?

Is there any way:

  • counts the number of lines (1 line = data line) in my small_db.php file
  • tell PHP to move the file pointer exactly to the beginning of line 322. (so that I could fgets () the data from this line)
  • tell PHP to return the line number or the entire content of the line in which the text "Fast brown fox" appears?

I know that there is another way: to get the entire contents of the file in one line, but, as you know, this method is inefficient in time and memory if the file size is large.

+4
source share
1 answer

To start with, here are a few key variables:

$filename = 'small_db.php'; $linenum = 322; $needle = 'Quick brown fox'; 
  • How to count the number of lines in a file?

    To do this quickly and easily, use count() after you open the file with file() :

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

    However, this will use large memory if your file is large, as it must load the entire file into memory in order to calculate the return value from file() . Alternatively, you can use the standard file functions fopen() , feof() , fgets() and fclose() to open the file and read it in turn, keeping the count when you go like this:

     $count = 0; $fp = fopen( $filename, 'r'); while( !feof( $fp)) { fgets( $fp); $count++; } fclose( $fp); 
  • How to move the file pointer to the beginning of a specific line number?

    This is best achieved using the SplFileObject class. You create a new object of this class with the name of the file that you open, then use seek() to find the line number of your choice. Then you can use key() to show the line number and current() (or getCurrentLine() or fgets() ) to get the contents of this line, for example:

     // 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(); 
  • How can I return the line number or the entire contents of the line in which a particular needle occurs?

    There are no PHP built-in functions / methods that can do this, as far as I know. You will need to analyze it yourself with something similar to this function, which checks each line in the file specified by its pointer to the $fp file, to the specific register $needle using strpos() (you can use stripos() to search without case sensitive):

     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) ); } 

    You will need to call this function as follows:

     $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); 
+12
source

All Articles