How to write to a file w/o overwritting text

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Jabolin
New php-forum User
New php-forum User
Posts: 4
Joined: Mon Dec 16, 2002 11:34 pm

How to write to a file w/o overwritting text

Postby Jabolin » Mon Dec 16, 2002 11:40 pm

Help I have an html file, and in that html file I located the spot that I wish to insert text. The problem is that I endup overwritting html that is below that spot. How can I make it not write over other text, but rather just push it farther down so that it does not overwrite previous code. Thanks

uncertain_manos
New php-forum User
New php-forum User
Posts: 31
Joined: Wed Oct 23, 2002 12:55 am

Postby uncertain_manos » Tue Dec 17, 2002 12:21 am

Jabolin wrote:Help I have an html file, and in that html file I located the spot that I wish to insert text. The problem is that I endup overwritting html that is below that spot. How can I make it not write over other text, but rather just push it farther down so that it does not overwrite previous code. Thanks


Jabolin,
from what you say, you have a spot in the html file. You need to find the text between the 2 spots (beginning and ending of the string) and then use the replace function to write the new text input in place.
You can do these two steps in the following way:
1) find the string to be replaced with the function "strpos ", check for details at http://www.php.net/manual/en/function.strpos.php

with this function you get the positions of those 2 spots. You then need to substract them and get the string in between with the function "substr" (again check foa mare details at http://www.php.net/manual/en/function.substr.php)

2) after that you have the string which you want to replace. At this point you use the the ereg_replace or eregi_replace according to whether you want the finding and replacement to be case sensitive or not. Check at http://www.php.net/manual/en/function.eregi-replace.php for more details.

One more point: Don;t forget to put in the string that will replace the original string the two spot points. It is a usual mistake and if you forget to do so, you will not be capable of replacing the strinf next time.

Hope it helps,
regards,
Manos

Jabolin
New php-forum User
New php-forum User
Posts: 4
Joined: Mon Dec 16, 2002 11:34 pm

Ummm kind of, but you pointed me in the right direction

Postby Jabolin » Tue Dec 17, 2002 11:31 pm

I could not get it to work quite that way. I ran into problems cause I figured that I would have to change the whole html document into one string, and then fix it afterwords--what a mess, and I could not see how to make that easy, so what I did was read one line at a time and if that was not the line that I wanted to replace I would print it to the new document, and then once I found the line I wanted to replace I would print that to the new document, and then go back to simply copying. This solved the problem. This also allows me to let the user preview the changes before actually re-writing the document. Thanks for pointing me in the right direction. Your help is greatly appreciated.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot], Google Feedfetcher and 1 guest