Jump to content

file reading and writing problem


warby12

Recommended Posts

Hi, I've written this function below to open a html file, read the file contents into a string, replace some stuff in the string and write it out again. But it is creating havoc, adding characters from the middle to the end and deleting stuff seemingly at random. I'm thinking it is possibly something to do with the way I am handling the file or a character encoding problem or an OS problem. Any help would be greatly appreciated.

 

 

 

 

// change text

$page_text = file_get_contents($file, filesize($file));

 

$temp_text =  $div .  "<H2><div name=\"item_date\">" . $date . "</div></H2>" . "<p><span class=\"text_style_1\"><div name=\"item_title\">" . $title . "</div></span>" .  "<div name=\"item_body\"><span class=\"text_style_cms\">" . $item . "</span></div>" .  "<div name=\"item_link\"><span class=\"text_style_cms\"><A href=\"" . $url . "\">" . $url . "</A></span></div>" .  "<div name=\"item_email\"><span class=\"text_style_cms\">" . $email . "</span></div>" ;

 

$page_text = str_replace ($div, $temp_text, $page_text);

// write file

$handle = fopen($file, 'w+b');

 

$bytes_chnged = fwrite($handle, $page_text);

$changed = fclose($handle);  ???

 

 

Link to comment
https://forums.phpfreaks.com/topic/112710-file-reading-and-writing-problem/
Share on other sites

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.