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);
Related
Currently I have a code, which displays data from a txt file, and randomizes it after converting it into an array.
$array = explode("\n", file_get_contents('test.txt'));
$rand_keys = array_rand($array, 2);
I am trying to make it so that, after this random value is displayed.
$search = $array[$rand_keys[0]];
We're able to store this into another txt file such as completed.txt and remove the randomized segment from our previous txt file. Here's the approach I tried, and surely didn't work out with.
$a = 'test.txt';
$b = file_get_contents('test.txt');
$c = str_replace($search, '', $b);
file_put_contents($a, $c);
Then to restore into a secondary file, I was messing with something like this.
$result = '';
foreach($lines as $line) {
if(stripos($line, $search) === false) {
$result .= $search;
}
}
file_put_contents('completed.txt', $result);
This actually appears to work to some extent, however when I look at the file completed.txt all of the contents are EXACTLY the same, and there's a bunch of blank spaces being left behind within test.txt
There are some better ways of doing it (IMHO), but at the moment you are just removing the actual line without the new line character. You may also find it will replace other lines as it just replaces the text without any idea of content.
But you will probably fix your code with the addition of replacing the new line...
$c = str_replace($search."\n", '', $b);
An alternative way of doing it is...
$fileName = 'test.txt';
$fileComplete = "completed.csv";
// Read file into an array
$lines = file($fileName, FILE_IGNORE_NEW_LINES);
// Pick a line
$randomLineKey = array_rand($lines);
// Get the text of that line
$randomLine = $lines[$randomLineKey];
// Remove the line
unset($lines[$randomLineKey]);
// write out new file
file_put_contents($fileName, implode(PHP_EOL, $lines));
// Add chosen line to completed file
file_put_contents($fileComplete, $randomLine.PHP_EOL, FILE_APPEND);
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);
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.
I have a text file and I want each line to be an element of an array.
$file = file("books.txt");
$split = explode("\n", $file);
Then if I try to print an element of the array:
echo "$split[0]";
I get no output.
Because file("books.txt") gives already an array resulting from exploding by newline, you can echo "$file[0]";, no need for further exploding.
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));