PHP File: Reading File at Particular Lines



PHP File Tips - Part 2: If you want to display or read particular line from file, you can use file(). Just remembering tips part 1, file() will generate array from file reading.
<?
$file = "test.txt";

$f = file($file);

// first line
echo $f[0];
// result test line 1 

echo "<br />";

// end line
echo end($f);
// result test line 3 

echo "<br />";

// second line
echo $f[1];
// result test line 2 

echo "<br />";

// line 2 - 3
$arr = array_slice($f,1,2);
$str = implode(",", $arr);
echo $str;
// result test line 2  test line 3 
?>


Series this article:
PHP File: 3 Way of Reading File
PHP File: Reading File at Particular Lines
PHP File: Reading File at Particular Byte Ranges
PHP File: Checking File or Directory
PHP File: Showing All File in A Directory
PHP File: Showing All Files and Folders in Particular Directory
PHP File: Retrieving File Statistic
PHP File: Retrieving More File Information
PHP File: Counting Lines in a File
PHP File: Counting Characters and Words in File
PHP File: Writing File
PHP File: Locking and Unlocking File
PHP File: Removing Lines From File
PHP File: Inserting Lines into a File


Tag: file, read file Category: PHP Basic Post : April 13th 2008 Read: 2,693 Bookmark and Share

blog comments powered by Disqus