Jump to content

Find and replace in text file - last characters missing


deanes02

Recommended Posts

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);
} 

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)) . ".";

}  

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...

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);
} ?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.