deanes02 Posted June 20, 2008 Share Posted June 20, 2008 Hi, The following code atempts to open a text file, search for a term, replace that term with a new string, and resave the file. It all works fine except when the file is resaved four characters from the end of the file are deleted. I think this is because the file I open will be, say 1000 characters long, whereas the file I resave will ALWAYS be four characters longer, 1004. However only the first 1000 characters will be saved. Is there any way to save all the characters? As I said the code works perfectly other than that. I hope this all makes sense. Thanks! function void_case_statement($search_for,$replace_term,$filename){ $search_for = "\"$search_for\""; $replace_term = "\"$replace_term\""; //echo "debug $search_for,$replace_term,$filename<br>"; //read contents and add new text $fh = fopen($filename, 'r')or die("FAILURE : can't open file"); $content = fread( $fh, filesize( $filename ) ); fclose($fh); //echo $content; $new_content = str_replace($search_for,$replace_term,$content); //echo $new_content; //write new contents to file $fh = fopen($filename, 'w') or die("FAILURE : can't open file"); //echo $new_content; fwrite($fh, $new_content); fclose($fh); } Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted June 20, 2008 Share Posted June 20, 2008 I don't see anything wrong with your file write code. If you are using php5 you can use the file_put_contents command, which is somewhat easier to read (and only one line). function void_case_statement($search_for, $replace_term, $filename){ $search_for = "\"$search_for\""; $replace_term = "\"$replace_term\""; $old_size = filesize($filename); //read contents and add new text $file = file_get_contents($filename); // update the file $new_size = file_put_contents($filename, str_replace($search_for, $replace_term, $content)); if ($new_size) { echo "File successfuly modified!"; } echo "Old file size was: " . $old_size . ".\n" . "New size is " . $new_size . ".\n" . "Expected size is: " . (($old_size - strlen($search_for)) + strlen($replace_term)) . "."; } Quote Link to comment Share on other sites More sharing options...
deanes02 Posted June 20, 2008 Author Share Posted June 20, 2008 Thanks for your reply. Just one slight error $new_size = file_put_contents($filename, str_replace($search_for, $replace_term, $content)); should read $new_size = file_put_contents($filename, str_replace($search_for, $replace_term, $file)); Anyway with that fixed the output is Old file size was: 54635. New size is 54639. Expected size is: 54637. And there are two characters missing. If I run it again I get Old file size was: 54759. New size is 54763. Expected size is: 54761. and there are an extra four characters missing from the end!!!! This is the same why it worked with my code too. Any other ideas? I think its something to do with the fact that I'm trying to find "MJ" [including the inverted commas] and replacing with "AR" [including inverted commas]. The term is replaced twice in each instance. The inverted commas are the problem??? PLEASE HELP! Thanks... Quote Link to comment Share on other sites More sharing options...
deanes02 Posted June 20, 2008 Author Share Posted June 20, 2008 Sorry ... the "AR" bit above should read "VOID" Quote Link to comment Share on other sites More sharing options...
kathas Posted June 20, 2008 Share Posted June 20, 2008 could you try the code below? i opened the file in binary mode and added the length parameter in fwrite so that it will ignore the magic_quotes_runtime configuration option and no slashes will be stripped. <?php function void_case_statement($search_for,$replace_term,$filename){ $search_for = "\"$search_for\""; $replace_term = "\"$replace_term\""; //echo "debug $search_for,$replace_term,$filename<br>"; //read contents and add new text $fh = fopen($filename, 'rb')or die("FAILURE : can't open file"); $content = fread( $fh, filesize( $filename ) ); fclose($fh); //echo $content; $new_content = str_replace($search_for,$replace_term,$content); //echo $new_content; //write new contents to file $fh = fopen($filename, 'wb') or die("FAILURE : can't open file"); //echo $new_content; fwrite($fh, $new_content,strlen($new_content)); fclose($fh); } ?> Quote Link to comment Share on other sites More sharing options...
deanes02 Posted June 20, 2008 Author Share Posted June 20, 2008 Thanks for the suggestion. The code runs without any errors but four character are still missing off the saved file. Each time I run it the new text goes in but another four characters are missing from the end. ??? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.