Jump to content

Edit a file


rupam_jaiswal

Recommended Posts

Hi,

Please excuse if similar query has been posted earlier.

 

I have text file in which I have to search a word and replace it with a new word.

 

First I do is to get the file contents with fopen,then read the contents , maintain a string $newcontent that holds the contents of file ,the moment i get my word,replace it with new word in the string $newcontent

After that I write the whole string $newcontent to file again.

What I want is to just while scanning  the file,the moment I find my word,replace it with new word there only instead of rewriting the whole file again.

Regards

Link to comment
https://forums.phpfreaks.com/topic/161804-edit-a-file/
Share on other sites

I'm not totally sure this is possible, with PHP at least. As I picture it in my head (excuse any weak terminology) if you were to overwrite part of the file, you'd be overwriting certain characters / bytes, but obviously if the string was a different length you'd be overwriting the characters next to it, or even leaving some as they were. Effectively you'd have to shuffle all the characters after it into place - which is almost rewriting the file anyway.

 

As I said though this is just how I picture it, I have nothing to back this up. There may well be PHP support for it... I've just never seen or read about it before. How comes it's such an issue?

Link to comment
https://forums.phpfreaks.com/topic/161804-edit-a-file/#findComment-853781
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.