Replacing string in file problems...

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 13
Joined: Tue Oct 29, 2002 2:58 am

Tue Apr 15, 2003 5:58 am


I have a small problem or big i u will...

i have a file and the contents of
the file is just date (ex: 2.2.2003).

now here the code to open that file
and get the contents into string:

$filename = "";
$fp = fopen ($filename, "r+");
$read_file = fread($fp, filesize ($filename));
$str_read = $read_file;
$str_date = "$date";
$str_read1 = str_replace($str_read, $str_datum, $str_read);
fputs ($fp, $str_read1);
fclose ($fp);

What's the problem? The problem is that this doesn't
replaces the contents of the file, it appends the
new date a line under the first.

here's the example:

contents of the file, before running code:

after running the code:

I just want to replace the first date (2.2.2003)
with the current date(3.2.2003).

See my problem? Intead of replacing the first date
with the current it appends it self a line under.

How to fox this?

I also tied with fwrite but it's the same s...

help me...


php-forum Active User
php-forum Active User
Posts: 264
Joined: Tue Apr 08, 2003 5:18 am
Location: Ottawa, Canada

Tue Apr 15, 2003 9:06 am

well this may be a barbaric suggestion of sorts but, once you get the date into memory, close the filestream, delete the file, and create a new file with $filename and then write new date to it....

Post Reply