View Full Version : Replacing line in text file
Hi,
is there any way to replace line in text file? I know how to clear line, but replacing it is problem to me. For example, I have file that looks like this:
lalalall|lalalla|lalla
kokokok|kokoko|kokoko
eeeee|eeee|eeee
and I want to replace line with red color with another text.
Thanks in advance.
define('LINE_BREAK', "\n"); // \n for *n?x, \r\n for Windows
function replaceLine($fileName, $oldLine, $newLine) {
$lines = file (http://www.php.net/file)($fileName);
for($i = 0; $i < count (http://www.php.net/count)($lines); ++$i)
if($lines[$i] == $oldLine)
$lines[$i] = $newLine;
$f = fopen (http://www.php.net/fopen)($fileName, 'w');
fwrite (http://www.php.net/fwrite)($f, implode(LINE_BREAK, $lines));
fclose (http://www.php.net/fclose)($f);
}
You might want to also see this thread (http://dynamicdrive.com/forums/showthread.php?t=11015).
I tried that, but isn't working...
Sorry, there was a broken BBCode in it.
I notified, but it weren't working... I solved the problem myself..
shachi
07-13-2006, 02:45 PM
liro: Would you like to share how you fixed it?? It's not a must of course. But I am having the same problem.:(
liro: Would you like to share how you fixed it?? It's not a must of course. But I am having the same problem.:(
Ok, here:
<?php
//Function for replacing line in text file.
//Credit: Iiro Krankka
function replaceLineInTextFile($file, $pattern, $replacement) {
if(!file_exists($file)) { // if file doesn't exist...
print "The specified file doesn't seem to exist."; // ...stop executing code.
} else { // if file exists...
$f = file($file); // ...make new variable...
$content; // ...and another...
for($i = 0; $i < count($f); $i++) { // ...run through the loop...
if(eregi($pattern, $f[$i])) { // and
$content .= $replacement . "\n"; // get
} else { // the
$content .= $f[$i]; // content.
}
}
$fi = fopen($file, "w"); // open specified file...
fwrite($fi, $content); // and rewrite it's content.
fclose($fi); // close file.
print "Line replaced!!!";
}
}
replaceLineInTextFile("online.txt", "1", "Found!"); // testing...
?>
I used it this way (variables in Finnish):
<?php
function replaceLine($f, $etsi, $uus) {
if(!file_exists($f)) {
die("Tiedostoa " . $f . " ei löytynyt.");
} else {
$filu = file($f);
$sisalto;
for($i = 0; $i < count($filu); $i++) {
list($id, $nimi, $passu) = explode("|", $filu[$i]);
if($id == $etsi) {
$sisalto .= $id . "|" . $uus . "\n";
} else {
$sisalto .= $filu[$i];
}
}
$fi = fopen($f, "w");
fwrite($fi, $sisalto);
fclose($fi);
}
}
replaceLine("online.txt", "43534", "haha|vaihto!!");
?>
and my online.txt file:
34344|hehe|passu
43433|hehe|passu
45342|hehe|passu
45646|hehe|passu
43534|haha|passu
76876|hehe|passu
76867|hehe|passu
34534|hehe|passu
P.S. Do You have Messenger? It would be great to share some scripts.... ;)
shachi
07-14-2006, 08:47 AM
Thanks liro, Twey.
liro: Yes I have messenger but I don't use them, I just use online messeging services like meebo, gmail and e-messenger.net. I will try to execute your script and see if it works for me.:)
liro: If you are talking about PHP scripts then I am not very good at it if javascript then I am not perfect at it.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.