I want to replace lines in a file.
Moderator: General Moderators
I want to replace lines in a file.
How do I open a file and replace lines with new lines in a file. I know about substr_replace(). But I can't figure out how to read a file, replace lines, then write them to a new file.
Re: I want to replace lines in a file.
file_get_contents - Get file contents, put it into a string.mwphurst wrote:How do I open a file and replace lines with new lines in a file. I know about substr_replace(). But I can't figure out how to read a file, replace lines, then write them to a new file.
substr_replace - Replace portions of string with another string
file_put_contents - output a string to a file. (Identical to calling fopen(), fwrite(), and fclose() successively)
All that is good advice but didn't really answer my question. I did find some info on preg_replace that worked. Not really how I wanted to do it but hey, wtf.
The problem with before and after files is they might already have other alterations that would be erased. Same goes for the line #. Any alterations would change the line numbers.
I'll probably do some more playing around with this. This is more of what I had in mind. Replacing one word with preg_replace might cause some problems. I would rather replace the whole string. My question here is how do I set it up?file_get_contents - Get file contents, put it into a string.
substr_replace - Replace portions of string with another string
file_put_contents - output a string to a file. (Identical to calling fopen(), fwrite(), and fclose() successively)
The problem with before and after files is they might already have other alterations that would be erased. Same goes for the line #. Any alterations would change the line numbers.
Okay, perhaps you need to state your needs more clearly. You asked:mwphurst wrote:All that is good advice but didn't really answer my question.
And I answered with links on the functions to read a file (into a string), replace lines (in a string), and to write (a string to) a file.mwphurst wrote: But I can't figure out how to read a file, replace lines, then write them to a new file.
As far as I can tell, that answered your question exactly.. and even in the same order as you asked.
Well, what whole string? You've given no example of input and desired output, which would really help. For example, if the file was like:mwphurst wrote: I would rather replace the whole string.
Code: Select all
blah blah blah їstuff] blah blahPlease clarify and elaborate on what you want to accomplish so we can help you accomplish it.
No. The alteration shouldnt change the line numbers (unless you have length limits on lines), and shouldnt change any other alterations - its an exact string match.mwphurst wrote: The problem with before and after files is they might already have other alterations that would be erased. Same goes for the line #. Any alterations would change the line numbers.
Like I said above, you are being very unclear. Provide some example input and desired output, and we'll explain how to write the filter.
I've done this type of thing many millenia ago, lol
the way I went about was I grabbed the contents of the entire file and put them in an array, with each newline signifying a new key in the array, then I changed the one line by corresponding it with its key in the array, then I re-wrote over all the file's contents with the array, after each key, there'd be a newline. the output: each line would be written over, but the only line that is different is the one that you change in the array, since the others are never changed, they are the same values when put back into the file
the way I went about was I grabbed the contents of the entire file and put them in an array, with each newline signifying a new key in the array, then I changed the one line by corresponding it with its key in the array, then I re-wrote over all the file's contents with the array, after each key, there'd be a newline. the output: each line would be written over, but the only line that is different is the one that you change in the array, since the others are never changed, they are the same values when put back into the file
OK here's what I have so far.
I have been able to get the first two to replace. I found I wasn't delimiting the ( ). Now I am having trouble getting the last one to work $patterns[3]. Not sure if it is a delimiting thing or not. I have tried backslashing everything. I took this straight from the example on http://us2.php.net/preg_replace$string = $contents;
$patterns[1] = "/header\(\"Location: login.php\"\);/";
$patterns[2] = "/include_once \(\"header.inc.php\"\);/";
$patterns[3] = "/<form action=\"orders.php?search=yes\" method=\"post\">/";
$replace[1] = "header(\"Location: user_login.php\");";
$replace[2] = "include_once (\"user_header.inc.php\");";
$replace[3] = "<form action=\"user_orders.php?search=yes\" method=\"post\">";
$replaced = preg_replace($patterns, $replace, $string);
$writing=fopen("$filename", "w");
fwrite($writing,$replaced);
fclose($writing);
fclose($handle);