How to Open a File from Line X to Line Y in PHP

How do I open a file from line X to line Y in PHP?

You not going to be able to read starting from line X because lines can be of arbitrary length. So you will have to read from the start counting the number of lines read to get to line X. For example:

<?php
$f = fopen('sample.txt', 'r');
$lineNo = 0;
$startLine = 3;
$endLine = 6;
while ($line = fgets($f)) {
$lineNo++;
if ($lineNo >= $startLine) {
echo $line;
}
if ($lineNo == $endLine) {
break;
}
}
fclose($f);

Want to read a file from line(1) to line(n) in php

Use simple logic with file function. Have a look on below solution:

if(isset($_POST['submit'])){

$selected_file = $_POST['files'];
$from_line = $_POST['from_line'];
$to_line = $_POST['to_line'];

$file_array = file($selected_file);

for($i = 0; $i < count($file_array); $i++){
if($i >= ($from_line -1) && $i < ($to_line)){
echo $file_array[$i];
echo '<br />';
}
}
}

It will echo required lines from the file.

Replace a whole line where a particular word is found in a text file

One approach that you can use on smaller files that can fit into your memory twice:

$data = file('myfile'); // reads an array of lines
function replace_a_line($data) {
if (stristr($data, 'certain word')) {
return "replacement line!\n";
}
return $data;
}
$data = array_map('replace_a_line', $data);
file_put_contents('myfile', $data);

A quick note, PHP > 5.3.0 supports lambda functions so you can remove the named function declaration and shorten the map to:

$data = array_map(function($data) {
return stristr($data,'certain word') ? "replacement line\n" : $data;
}, $data);

You could theoretically make this a single (harder to follow) php statement:

file_put_contents('myfile', implode('', 
array_map(function($data) {
return stristr($data,'certain word') ? "replacement line\n" : $data;
}, file('myfile'))
));

Another (less memory intensive) approach that you should use for larger files:

$reading = fopen('myfile', 'r');
$writing = fopen('myfile.tmp', 'w');

$replaced = false;

while (!feof($reading)) {
$line = fgets($reading);
if (stristr($line,'certain word')) {
$line = "replacement line!\n";
$replaced = true;
}
fputs($writing, $line);
}
fclose($reading); fclose($writing);
// might as well not overwrite the file if we didn't replace anything
if ($replaced)
{
rename('myfile.tmp', 'myfile');
} else {
unlink('myfile.tmp');
}

fgetcsv open at specific row?

There's no simple way to start reading at row 100, but what you can do is read/set the position in the file like this:

$k = <previous position>;
fseek($file, $k);
while(...) {
fgetcsv(...);
...
}
$k = ftell($file); //new position

Now it's just a matter of preserving this position between page calls.

PHP Grab last 15 lines in txt file

Try using array_slice, which will return a part of an array. In this case you want it to return the last 15 lines of the array, so:

$filearray = file("filename");
$lastfifteenlines = array_slice($filearray,-15);

How to read specific lines from a file (by line number)?

If the file to read is big, and you don't want to read the whole file in memory at once:

fp = open("file")
for i, line in enumerate(fp):
if i == 25:
# 26th line
elif i == 29:
# 30th line
elif i > 29:
break
fp.close()

Note that i == n-1 for the nth line.


In Python 2.6 or later:

with open("file") as fp:
for i, line in enumerate(fp):
if i == 25:
# 26th line
elif i == 29:
# 30th line
elif i > 29:
break


Related Topics



Leave a reply



Submit