By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

trying to edit a txt file

P: n/a
Hi I am trying to edit a text file... the format of which is like

#####abc.conf
poll somethin
proto pop3
via 127.9.02.1
user "me@me.com"
pass "dell"
is me.nutcase
preconnect "abc abc abc abc"
##
poll ak #
proto pop3
via 11.11.22.33
user "ak@bk.com"
pass "eatme"
is nutter
fetchall
preconnect "abs -e -f -f /ss/ss/ "
##

The way i am tryin to edit this file is ....

<?php
$file = "/som/thing/abc.conf"
$dele = "poll ak";
$open = fopen($file, "r+w");
$read = fread($open, filesize($file));
$sep1 = explode($dele, $read);
$sep2 = explode('##', $sep1[1]);

$read = str_replace(array($dele, $sep2[0]), array("", "\n"), $read);
fseek($open, 0);
fwrite($open, $read);
fclose($open);

The result of what happens is it delete the tag from poll ak till the ##

but also adds some text at file end like ....
Resource id #2Resource id #2Resource id #2Resource id #2Resource id
#2Resource id #2Resource id #2Resource id #2Resource id #2Resource id
#2Resource id #2Resource id #2Resource id #2Resource id #2Resource id
#2Resource id #2Resource id #2Resource id #2Resource id #2Resource id
#2Resource id #2Resource id #2Resource id #2Resource id #2Resource id
#2Resource id #2Resource id #2Resource id #2Resource id #2Resource id
#2Resource id #2Resource id #2Resource id #2Resource id #2Resource id
#2Resource id #2Resource id #2Resource id #2Resource id #2Resource id
#2Resource id #2Resource id #2defaults
via 11.11.22.33
user "ak@bk.com"
pass "eatme"
is nutter
fetchall
preconnect "abs -e -f -f /ss/ss/ "
Any suggestions....
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
IronMonk wrote:
<?php
$file = "/som/thing/abc.conf"
$dele = "poll ak";
$open = fopen($file, "r+w");
$read = fread($open, filesize($file));
$sep1 = explode($dele, $read);
$sep2 = explode('##', $sep1[1]);

$read = str_replace(array($dele, $sep2[0]), array("", "\n"), $read);
fseek($open, 0);
# Not tested
ftruncate($open, 0);
fwrite($open, $read);
fclose($open);


?>

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.