file()
Where file_get_contents() gave you the contents of a file as a string, this function will give you the same in an array. Each line of the file will be one element of the array (as a string) with the new line still at the end.
The following flags may be used as the second parameter.
FILE_USE_INCLUDE_PATH: Search for the file in the include directory.
FILE_IGNORE_NEW_LINES: The new line character will not appear at the end of each array element
FILE_SKIP_EMPTY_LINES: Empty lines in the file will not appear in the array
FILE_TEXT (since PHP 5.2.7): Data is read in text mode.
FILE_BINARY (since PHP 5.2.7): Data is read in binary mode.
The third parameter is for a stream context (see manual).
For this example, we’ll use the file created in our first file_put_contents() example.
All the laddies cat call and wolf whistle So-called gentlemen and ladies Dog fight like rose and thistle
$file = 'output.txt'; $data = file( $file ); /* array ( 0 => 'All the laddies cat call and wolf whistle ', 1 => 'So-called gentlemen and ladies ', 2 => 'Dog fight like rose and thistle' ) */ $data = file( $file, FILE_IGNORE_NEW_LINES ); /* array ( 0 => 'All the laddies cat call and wolf whistle', 1 => 'So-called gentlemen and ladies', 2 => 'Dog fight like rose and thistle' ) */ // now let's add some new lines with a blank line too $new = "\nI've got a feeling I'm gonna get a lot of grief Once this seemed so appealing Now I am beyond belief "; file_put_contents( $file, $new, FILE_APPEND ); $data = file( $file ); /* array ( 0 => 'All the laddies cat call and wolf whistle ', 1 => 'So-called gentlemen and ladies ', 2 => 'Dog fight like rose and thistle ', 3 => 'I\'ve got a feeling ', 4 => 'I\'m gonna get a lot of grief ', 5 => ' ', 6 => 'Once this seemed so appealing ', 7 => 'Now I am beyond belief ' ) */ $data = file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ); /* array ( 0 => 'All the laddies cat call and wolf whistle', 1 => 'So-called gentlemen and ladies', 2 => 'Dog fight like rose and thistle', 3 => 'I\'ve got a feeling', 4 => 'I\'m gonna get a lot of grief', 5 => 'Once this seemed so appealing', 6 => 'Now I am beyond belief' ) */
