Page 1 of 1
I want to replace lines in a file.
Posted: Mon Jan 19, 2004 1:08 pm
by mwphurst
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.
Posted: Mon Jan 19, 2004 1:12 pm
by dull1554
thats a tough one, I tried it when i was a newb, but after many failed attempts, i forced myself to learn mysql......made my life a heck of alot simpler
Posted: Mon Jan 19, 2004 1:17 pm
by mwphurst
I was able to get it working back in cgi. I am doing some upgrades for certain scripts for other people. I was hoping to create an install script to alter lines so people won't have to do it themselves.
Posted: Mon Jan 19, 2004 1:50 pm
by dull1554
yeah like i said, it's a tough one, personally i'd have two files a pre-install file, and a post-install file, then just replace one with the other
Re: I want to replace lines in a file.
Posted: Mon Jan 19, 2004 2:06 pm
by Roja
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.
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)
Posted: Mon Jan 19, 2004 2:21 pm
by patrikG
If you know the line-number it's easy: [php_man]file[/php_man] reads the contents of a file into an array, each line being a new array-element.
Posted: Mon Jan 19, 2004 2:32 pm
by mwphurst
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.
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)
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?
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.
Posted: Mon Jan 19, 2004 3:26 pm
by Roja
mwphurst wrote:All that is good advice but didn't really answer my question.
Okay, perhaps you need to state your needs more clearly. You asked:
mwphurst wrote:
But I can't figure out how to read a file, replace lines, then write them to a new file.
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.
As far as I can tell, that answered your question exactly.. and even in the same order as you asked.
mwphurst wrote:
I would rather replace the whole string.
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:
Code: Select all
blah blah blah їstuff] blah blah
Then you could replace [stuff] trivially easily using just the functions I posted. If however you want to do something like replace the second blah, and instances of [stuff] that are followed by blah, that might take some effort.
Please clarify and elaborate on what you want to accomplish so we can help you accomplish it.
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.
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.
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.
Posted: Mon Jan 19, 2004 3:32 pm
by Meteo
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
Posted: Mon Jan 19, 2004 3:52 pm
by mwphurst
OK here's what I have so far.
$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);
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