This question already has answers here:
Replace string in text file using PHP
(4 answers)
Closed 1 year ago.
i need to find a line and change it to whatever user will type in input i didnt write this code pls help
<?php
$myfile = 'test.txt';
$lines = file($myFile, FILE_IGNORE_NEW_LINES);
$lines[4] = $_POST['title'];
file_put_contents($myFile , implode("\n", $lines));
?>
i tryed lot of ways but non of them works and i only have this code . i am working on project idea is simple user will give type url of website i need to find a line and change it to <form action="process.php" method="post"> i need help its will be on same line so i dont need to change line
i searched lot of ways but does not works for me.
i am starter at php so...
There is an error in your code.
$lines = file($myFile, FILE_IGNORE_NEW_LINES);
should be
$lines = file($myfile, FILE_IGNORE_NEW_LINES);
because you're declaring
$myfile = 'test.txt';
Note the usage of smaller case f in file name.
Now, coming to your question.
You want to replace some lines in the file with user input.
Let's assume that you want to replace the text THIS SHOULD BE REPLACED.
For that, you could use the code something line below:
<?PHP
$myfile = './test.txt';
$lines = file($myfile, FILE_IGNORE_NEW_LINES);
$textToBeReplaced = "THIS SHOULD BE REPLACED";
$index = array_search($textToBeReplaced, $lines);
if (false !== $index) {
$lines[$index] = $_POST['title'] ?? '';
}
file_put_contents($myFile , implode("\n", $lines));
?>
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);
This question already has answers here:
How to Remove Some Line from Text File Using PHP?
(2 answers)
Closed 7 years ago.
`<?php
$aclname = $_POST['aclname'];
$file = file_get_contents("test.txt");
$lines = explode("\n", $file);
$exclude = array();
foreach ($lines as $line) {
if (strpos($line, $aclname) !== FALSE) {
continue;
}
$exclude[] = $line;
}
echo implode("\n", $exclude);
?>
Please help me with a code to open a file. Find a word in a file which matches the input variable from html, and then delete the entire string.
$word = $_POST['word'];
possible matches of $word from the file should be found and the entire string should be deleted.
example:
input
$word = hello
strings in the file
hello world
hi world
how are you world.
output
hi world
how are you world
You will need to read the entire content of that file
$str=file_get_contents('file.txt');
Then replace the message you want to delete
//replace something in the file string - this is a VERY simple example
$str=str_replace("$oldMessage", "$deletedFormat",$str);
Then write it on the file
//write the entire string
file_put_contents('file.txt', $str);
You can use preg_replace for replacing value.
$word = $_POST['word'];
$data = file_get_contents($filename);
$data = preg_replace($word, '', $data);
file_put_contents($filename, $data);
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 got a simple php script that is storing the content of POST in to a text file. data.txt looks like this:
Something - Line1
Something - Line2
Something - Line3
Something - Line4
I'm trying to figure out the best way to set each line as a variable e.g.
$line1 = "Something - Line1";
$line2 = "Something - Line2";
$line3 = "Something - Line3";
$line4 = "Something - Line4;"
so I can use it for further processing. Please note that the numbers of line can change. Any ideas? :-)
Thanks!
Best to use an array. Check out file():
$lines = file('/path/to/file.txt');
Then you access the lines starting from 0:
echo $lines[0];
echo $lines[1];
//etc...
Or loop through them:
foreach($lines as $line) {
echo $line;
}
This link provides your answer: Read each line of txt file to new array element
The link makes use of the while loop and searches for the end of the file
The link provided uses the following code:
<?php
$file = fopen("members.txt", "r");
$members = array();
while (!feof($file)) {
$members[] = fgets($file);
}
fclose($file);
var_dump($members);
?>
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php - efficent way to get and remove first line in file
I need to remove first line form txt file using php. Could you show me example code how to do it? I know that it's easy but I don't know php:) Thanks!
You could do this:
$file = file_get_contents(SOME_FILE);
$arr = explode('\n\r', $file);
if (isset($arr[0])) unset ($arr[0]);
$string = implode('\n\r', $arr);
file_put_contents(SOME_FILE, $string);
You could have used the search function at least. You would have found this:
$contents = file($file, FILE_IGNORE_NEW_LINES);
$first_line = array_shift($contents);
file_put_contents($file, implode("\r\n", $contents));