Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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