get the first 3 lines of a text file in php [duplicate] - php

This question already has answers here:
How to read a large file line by line?
(14 answers)
Closed 8 years ago.
I am developing a website in PHP, and I must include in the index the first 3 lines of a text file in PHP. How can I do that?
<?php
$file = file_get_contents("text.txt");
//echo the first 3 lines, but it's wrong
echo $file;
?>

Even more simple:
<?php
$file_data = array_slice(file('file.txt'), 0, 3);
print_r($file_data);

Open the file, read lines, close the file:
// Open the file for reading
$file = 'file.txt';
$fh = fopen($file, 'rb');
// Handle failure
if ($fh === false) {
die('Could not open file: '.$file);
}
// Loop 3 times
for ($i = 0; $i < 3; $i++) {
// Read a line
$line = fgets($fh);
// If a line was read then output it, otherwise
// show an error
if ($line !== false) {
echo $line;
} else {
die('An error occurred while reading from file: '.$file);
}
}
// Close the file handle; when you are done using a
// resource you should always close it immediately
if (fclose($fh) === false) {
die('Could not close file: '.$file);
}

The file() function returns the lines of a file as an array. Unless the file is huge (multiple megabytes), you can then use array_slice to get the first 3 elements of this:
$lines = file('file.txt');
$first3 = array_slice($lines, 0, 3);
echo implode('', $first3);

Related

Read specific line from csv file on php [duplicate]

This question already has answers here:
PHP dynamically create CSV: Skip the first line of a CSV file
(8 answers)
Closed 3 years ago.
How do I get the second line from this CSV file?
<?php
$file = file('list.csv');
while (($line = fgetcsv($file)) !== FALSE) {
print_r($line);
}
?>
You can use an iterator to apply condition
$file = file('list.csv');
$i = 0;
while (($line = fgetcsv($file)) !== FALSE) {
if($i == 1){
print_r($line);
break;
}
$i++
}
Firstly file() reads the file into an array, so you don't need that - just open it using fopen() (and don't forget to close it), then if you only want the second line, then just call fgetcsv() twice...
$file = fopen('list.csv', r);
$line = '';
if (fgetcsv($file) !== FALSE) {
$line=fgetcsv($file);
}
fclose($file);

How do i read first 5 lines in php?

I am trying to read the first 5 line code-block in txt file, please how do i do this
I have this php code to get only the first line
<?php
$file = 'example.txt';
$f = fopen($file, 'r');
$line = fgets($f);
while (($line = fgets( $f)) !== false) {
for ($list = 1; $list < 6; $list++){
$codeline= htmlentities($line );
}
}
fclose($f);
?>
You can use a for loop:
for ($x = 1; $x < 6; $x++) {
$line = fgets($f);
}
To open and read a file line by line:
$file = fopen( "/path/to/file.txt", "r" );
$index=0;
while ((( $line = fgets( $file )) !== false) && ( $index++ < 5 )) {
echo $line;
}
fclose( $file );
Here, I am initializing a variable index to 0.
In the while loop, we will use fgets to read the next line of the file, and assign it to the variable line. We will also check that the value of index is less then 5, our desired line count, in addition to incrementing the value of the index, after we have read the line.
Once the value of index has reached > 5, the loop will exit, and the file stream will be closed.
The advantage of using fopen and fgets over something like file, is that the latter will load the entire contents of the file into memory - even if you do not plan on using the whole thing.
With a multi line file, the above code will print out the first five lines.
This is a multi line
Even more simple:
<?php
$file_data = array_slice(file('file.txt'), 0, 5);
print_r($file_data);
source: get the first 3 lines of a text file in php from #paul-denisevich

how to delete first line from a text file in php [duplicate]

This question already has answers here:
Is this the most efficient way to get and remove first line in file?
(12 answers)
Closed 6 years ago.
I try to make a simple php to read and delete first line from a text file.
So far I got the code:
<?php
$myfile = fopen("text.txt", "r") or die("Unable to open file!");
$ch=1;
while(!feof($myfile)) {
$dataline= fgets($myfile);
$listes = explode("\n",file_get_contents("text.txt"));
$poc = $listes[0];
if($ch == 2){
$gol = str_replace(' ', ' ', $dataline)."\n";
$fh = fopen("newtext.txt",'a');
fputs($fh, $gol);
fclose($fh);
chmod("newtext.txt", 0777);
}
$ch = 2;
}
unlink("text.txt");
copy("newtext.txt", "text.txt");
chmod("text.txt", 0777);
unlink("newtext.txt");
fclose($myfile);
echo $poc;
flush();
?>
The code is working only for the first two lines in text.txt but when it should read the third line the code stop working. Any advice please.
$handle = fopen("file", "r");
$first = fgets($handle,2048); #get first line.
$outfile="temp";
$o = fopen($outfile,"w");
while (!feof($handle)) {
$buffer = fgets($handle,2048);
fwrite($o,$buffer);
}
fclose($handle);
fclose($o);
rename($outfile,$file);
credits to ghostdog74 for this solution link

Read a line in file txt in PHP [duplicate]

This question already has answers here:
PHP: Read Specific Line From File
(13 answers)
Closed 7 years ago.
I want read a line in file text. EX line 5. Any body can help me????
$fp=fopen("test.txt",r)or exit("khong tim thay file can mo");
while(!feof($fp)){
echo fgets($fp);
}
fclose($fp);
Thanks for read
You can use the fgets() function to read the file line by line:
<?php
$handle = fopen("test.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo $line.'<br/>';
}
fclose($handle);
} else {
// error opening the file.
}
?>
$myFile = "text.txt";
$lines = file($myFile);//file in to an array
echo $lines[1]; //line 2
PHP - Reads entire file into an array
Just put an incremental counter in your loop, only echo if that counter matches your requred line number, then you can simply break out of the loop
$required = 5;
$line = 1;
$fp=fopen("test.txt",r)or exit("khong tim thay file can mo");
while(!feof($fp)){
if ($line == $required) {
echo fgets($fp);
break;
}
++$line;
}
fclose($fp);

php reading from file and manipulating the data

I am having some difficulty with reading info from a text file. Is it possible to use php and get one line at a time, and compare that line to a variable, one character at a time? Every time I add the character searching algorithm it messes up. or does the file reading only do full files/lines/character
ex:
$file=fopen("text/dialogue.txt","r") or exit("unable to open dialogue file");
if($file == true) {
echo "File is open";
fgets($file);
$c = "";
while(!feof($file)) {
$line = fgets($file)
while($temp = fgetc($line)) {
$c = $c . $temp;
//if statement and comparrison
}
}
} else {
echo "File not open";
}
fclose($file);
You may use php file function to read a file line by line
<?php
$lines = file("myfile.txt");
foreach($lines as $line){
## do whatever you like here
echo($line);
}
?>
Please check php manual
http://php.net/manual/en/function.file.php

Categories