Re: Need help: change a string in a file

From: Date: Wed, 06 Apr 2005 17:23:28 +0000
Subject: Re: Need help: change a string in a file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-212456@lists.php.net to get a copy of this message
I need some help in searching string in a file. My requirement is to search the string,append something to the string . Write the string back to the file without changing other parts of the file. e.g Suppose I have the following string in a file Set-Cookie: T_COOKIE=127.0.0.1-saswat-9; path=/ The fixed string is "Set-Cookie: T_COOKIE="and rest of the string is variable. I would like to change the string "127.0.0.1-saswat-9" to say "127.0.0.1-saswat-9-1234" My constraint is : I don't want to read the whole file into a buffer and work on that buffer as the file can be very large , sometimes more than 10 mb. My advantage is : I know that I will get the search string in first 200 bytes of the file , so I just need to read 200 bytes,play with that and write it back (overwriting the first 200 bytes with new 200 some bytes) .. Would appreciate any help or pointers.
Something like this (completely un-checked)... $fd = fopen("file", "r+"); $buf = fread($fd, 200); // modify $buf... fseek($fd, 0); fwrite($fd, $buf, 200); fclose($fd); You might want to add some flock() stuff in there as well... -philip

« previous php.general (#212456) next »