i have a file called "test.txt"
this is what it looks like
6
new
old
now
then
why
what
i want to change only the first line, that is change number 6 to any other number. How can i do this?
| SitePoint Sponsor |


i have a file called "test.txt"
this is what it looks like
6
new
old
now
then
why
what
i want to change only the first line, that is change number 6 to any other number. How can i do this?
You can't edit one line in a file, what you have to do:
Open your file in php
Put your file in a variable
$variable = ereg_replace("6",$other_number,$variable);
and then replace the whole file:
PHP Code:$file = "name_of_your_file.txt";
$fe = fopen($file,"w");
$write = fwrite($fe, $variable);
$write .= fclose($fe);
if(!$write){
echo"Error while writing to \"$file\"";
}
else{
echo"Updated!";
}


Hi iBram007,
Thank you very much for the code, but the number 6 was just a sample of the number that will be there. I cant keep track of the number.So I cant do a ereg_replace.
![]()





This ought to serve your purpose, it basically reads the entire text file into an array and then loops through it replacing the first element with whatever you choose adn then builds a new array. Then it writes that new array back to the text file.
PHP Code:<?
$in = file("test.txt");
$newnumber = 10;
$counter = 0;
foreach($in as $key => $line) {
if($counter == 0) {
$line = $newnumber;
}
$out[] = chop($line);
$counter++;
}
$outfile = fopen("test.txt", "w");
foreach($out as $key => $line) {
fputs($outfile, $line."\n");
}
fclose($outfile);
?>
Please don't PM me with questions.
Use the forums, that is what they are here for.





Well, then how are you determining which part/line/number you want to edit? Need a bit more info here.Oh, and if you were going to replace 6, for example, I'd recommend str_replace(), seeing as how a regular expression would not be necessary. It's a tiny bit quicker.





ereg_replace() and str_replace are no use here all he wants to do, at least from his first post is to replace the very first line of the text file. I think my method above will be the quickest way top achieve this. Although I would love for someone to prove me wrong here![]()
Please don't PM me with questions.
Use the forums, that is what they are here for.





Not at all -- I wasn't really attempting to solve the problem, just wanted to note that, if he were going to replace a specific number like that, as someone else mentioned, it'd be better to use that function. As for his post: heck, I'm stumped. He says the first line of the file, but I don't know if he means the first line EVERY time, or just in this example. I dunno.![]()


I need to replace the conent of the first line of the file only. Nothing else.
I tried the code freddy gave me. I get this error
Warning: Failed opening '/path/to/file/index.php' for inclusion (include_path='') in Unknown on line 0
i have changed the path to the file when posted here. But the the path in the script is correct.
Do i need to chmod any files?





Can you post the code you tried?
Please don't PM me with questions.
Use the forums, that is what they are here for.


<?
$file = "/home/boo/public_html/test.txt";
$in*=*file($file);
$newnumber*=*10;
$counter*=*0;
foreach($in*as*$key*=>*$line)*{
****if($counter*==*0)*{
********$line*=*$newnumber;
********}
****$out[]*=*chop($line);
****$counter++;
****}
$outfile*=*fopen($file,*"w");
foreach($out*as*$key*=>*$line)*{
****fputs($outfile,*$line."\n");
****}
fclose($outfile);
?>





That should work, are you sure you have the path right? The file must be wriatble by the web server for this work. try chmod the file to 777 or 766.
Please don't PM me with questions.
Use the forums, that is what they are here for.


<?
$file = "/home/unreg/public_html/traffic/site.txt";
$t = file($file);
$total_links = count($t);
$to_visit = $t[0];
$new_visit = $to_visit+1;
if ($new_visit > $total_links) {
$new_visit = 1;
}
$new_array[0] = "$new_visit";
for ($i=0;$i<$total_links;$i++) {
$new_array[$i] = $t[$i];
}
$outfile*=*fopen($file,*"w");
for ($l=0;$l<$total_links;$l++) {
fputs($outfile,*$new_array[$l]."\n");
**** }
fclose($outfile);
echo "success..";
?>
ok.. here is the exact code i am usingits giving me a parser error, and i cant find where it is...
Thanks a lot for the help so far.





What exactly does the error say?
Please don't PM me with questions.
Use the forums, that is what they are here for.


the error says,
Parse error: parse error in /home/unreg/public_html/index.php on line 17
but i cant find anything wrong with it![]()
Last edited by unregistered1; Aug 15, 2001 at 22:57.





Hmmm.. weird I just tried your exact code and it worked as expected. No parse error. Except here it adds an extra line break between each link because you need to use chop() when assigning the lements to the new array like.
$new_array[$i] = chop($t[$i]);
What platform are you running php on?
Please don't PM me with questions.
Use the forums, that is what they are here for.


red hat linux.





Are you sure you are viewing the most current copy of the page, because like said I copy and pasted that exact code and it works for me.
Please don't PM me with questions.
Use the forums, that is what they are here for.


freddy, its the most currect version.I have tried it on 5 different red hat servers. I get the same error.
![]()





I don't know then.
//In action
http://www.shtreasures.com/spftest.php
//Source Code
http://www.shtreasures.com/spftest.phps
Please don't PM me with questions.
Use the forums, that is what they are here for.


your not gonna believe this.. but when i copied the source from your page.. it worked... really freaky..![]()
Bookmarks