Modify Line n With PHP? - php

I want to write on line 5 of my file.
I can't use replace because my file use many numbers in lines.
So I need to know how can modify line 5.
You know we can read line 5 of file with:
$File='myfile.txt';
$readline = file($File);
$line5=$readline[4];'
I need to code that can modify only line 5 of $File.

In your code $read is undefined. It should be $readline, the following should work for you:
$File = 'myfile.txt';
$lines = file($File);
$lines[4] = 'modified line';
file_put_contents($File, implode("\r\n", $lines));

Related

How do I put content on second line in .txt file and read it

For storing values in a .txt file I use this code:
file_put_contents('data/vote_result.txt', implode(',', $results));
and for reading i use this:
$results = explode(',', file_get_contents('data/vote_result.txt'));
The content of vote_result.txt looks like this: 0,1,2,3
How can I store a second line in the same .txt file so that the content looks like this:
0,1,2,3
0,1,2,3
and how can I read that second line?
Read second line:
$myFile = "data/vote_result.txt";
$linesArray = file($myFile);
echo $linesArray[1]; //line 2
If you want to append a line to file, use FILE_APPEND flag in file_put_contents and concatenate "\n" with implode.
file_put_contents('data/vote_result.txt', implode(',', $results)."\n", FILE_APPEND);
Besides that you should use a database like MySQL for this, you can use the file function. Example:
$data = file('file.txt');
print $data[1]; // printing out the second line
This given you can simply add new lines just by adding a new entry in the array and then implode it with the newline character and save it via the file_put_contents function.
$content = implode("\n", $data);
file_put_contents('file.txt', $content);

Overwrite a specific line in a text file with php [duplicate]

This question already has answers here:
Replace a whole line where a particular word is found in a text file
(12 answers)
Closed last year.
Need to find a simple solution to the following:
I have a php file that, when executed, should be able to replace a specific line of code within itself, with a different value on that line.
I came up with this so far:
$file = "file.php";
$content = file($file);
foreach($content as $lineNumber => &$lineContent) {
if($lineNumber == 19) {
$lineContent .= "replacement_string";
}
}
$allContent = implode("", $content);
file_put_contents($file, $allContent);
However, that does not replace the specific line. It adds the new string on a new line and that's it. I need that specific line ERASED and then REPLACED with the new string, on that line.
How would I continue about doing that? I'd love some pointers.
Since file() creates an array, make use of the index to select the line.
Don't forget that array indexes start at 0!
$file = "file.php";
$content = file($file);
$content[19] = "replacement_string\r\n";
$allContent = implode("", $content);
file_put_contents($file, $allContent);
Your problem is the .= in the $lineContent .= "replacement_string"; line. Just use = or use the str_replace() or str_ireplace() function.

file put contents append before last second line

1st line
2nd line
...
//append here
last line
I have a php script use file put content append
I need to append between last 2nd line & last line?
anyone know how to achieve this?
Alternatively, you could use file(), and outputs an array. From there you can determine the second to the last line. Consider this example:
$replacement = "Hello World";
$contents = file('file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$size = count($contents);
$contents[$size-2] = $replacement; // point it to the second last line and assign
$temp = implode("\n", $contents);
file_put_contents('file.txt', $temp);

PHP, skip first and last line of a text file using fgets

I'm trying to figure out how to skip the first and last line of a text file when I'm reading it while using fgets(). The first line could be solved with an if(!$firstLine), but I'm not sure how to ignore the last line or if my solution for ignoring the first line is the best choice.
fgets($file); //Ignore the first line
$line = fgets($file);
$next = fgets($file);
while ($next !== false) { //check the line after the one you will process next.
//This way, when $next is false, then you still have one line left you could process, the last line.
//Do Stuff
$line = $next;
$next = fgets($file);
}
$lines = file('http://www.example.com/');//or local file
unset($lines[0]);
unset($lines[count($lines)-1)]);
$new_file = implode('', $lines); //may want to use line break "\n" for the join
You might want to try exploding the file contents. It will help you alot! After that you will label it.
Unfortunately, file() will open file into "serial" stream (not sure if serial is right word), it means that you must read full file before detecting his end
try this
$str= file_get_contents("file.txt");
$arr = preg_split ("\n",$str);
$n = count($arr);
$n is a number of lines
$str="";
for($i=0;$i<$n;$i++){
if($i>0 && $i<($n-1)){ $str.=$arr[$i]}
}
/**Str without line 1 and final line */

How do I read a text file into array of lines and display it using PHP?

I have text file that looks like this:
1 1 1 1
1 2 3 5
4 4 5 5
I want to read this text file into array of lines and display it. Can anyone help me do this?
This should get you going: php function file
<?php
$cont = file_get_contents("data.txt");
$lines = explode("\n",$cont); // $lines is now an array containing each line
// do something with data here
?>
make sure you use the correct line endings however as Windows uses \r\n and UNIX uses \n.
you can use fopen(), fgets(). see here
eg
$f=fopen("file","r");
if($f){
while (!feof($f)) {
$line = fgets($f, 4096);
print $line;
}
fclose($f);
}
You'd want to do something like this:
<?
$filename = "somefile.txt";
$arr = file($filename);
foreach($arr as $line){
print $line . "<br />";
}
?>
This one is working for me.
$array = explode("\n", file_get_contents('file.txt'));

Categories