
Originally Posted by
shachi
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 Code:
<?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 Code:
<?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:
Code:
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....
Bookmarks