Current location: Hot Scripts Forums » Programming Languages » PHP » PHP and TXT files


PHP and TXT files

Reply
  #1 (permalink)  
Old 11-02-03, 10:32 AM
JJSmallballer JJSmallballer is offline
Newbie Coder
 
Join Date: Oct 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
PHP and TXT files

I want to make a script that deletes a certain word from a txt file. I have a flash chat, and when you press logout, it runs a script on php. Here is the entire logout.php file:

Code:
<?
$nickname = str_replace ("\n"," ", $nickname);
$nickname = str_replace ("<", " ", $nickname);
$nickname = str_replace (">", " ", $nickname);
$nickname = stripslashes ($nickname);
?>

<?
$fp = fopen($chat_file, "a+");
$a = count($nickname);
$fd = delete($fp, $a, $nickname);
$fw = fclose($fp);
}
?>
Well, it doesn't work... so any ideas?
Reply With Quote
  #2 (permalink)  
Old 11-02-03, 11:43 AM
JJSmallballer JJSmallballer is offline
Newbie Coder
 
Join Date: Oct 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by JJSmallballer
I want to make a script that deletes a certain word from a txt file. I have a flash chat, and when you press logout, it runs a script on php. Here is the entire logout.php file:

Code:
<?
$nickname = str_replace ("\n"," ", $nickname);
$nickname = str_replace ("<", " ", $nickname);
$nickname = str_replace (">", " ", $nickname);
$nickname = stripslashes ($nickname);
?>

<?
$fp = fopen($chat_file, "a+");
$a = count($nickname);
$fd = delete($fp, $a, $nickname);
$fw = fclose($fp);
}
?>
Well, it doesn't work... so any ideas?
Changed it a little...

PHP Code:

<?

$nickname 
str_replace ("\n"," "$nickname);
$nickname str_replace ("<"" "$nickname);
$nickname str_replace (">"" "$nickname);
$nickname stripslashes ($nickname);


$chat_file "login.txt";
$fp fopen($chat_file"a+");
$a preg_match($nickname);
$fd delete($nickname);
$fw fclose($fp);
fopen($chat_file$a$fd$fw)

}
?>
Reply With Quote
  #3 (permalink)  
Old 11-02-03, 11:58 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
I believe the function you are looking for is:

PHP Code:

string str_replace(string wordstring replacementstring loginfile); 

where "word" is the word you want to find in your file, "replacement" is what you want to replace it with, and "loginfile" is the string that contains the contents of your login file. In other words, read the login file first and assign the contents of it to a string variable. Then search for the word you want to replace inside of the loginfile string using the string replace function above and then write the content of the login file back to the file.
__________________

Last edited by digioz; 11-02-03 at 12:03 PM.
Reply With Quote
  #4 (permalink)  
Old 11-02-03, 07:20 PM
JJSmallballer JJSmallballer is offline
Newbie Coder
 
Join Date: Oct 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
That didn't work...
Reply With Quote
  #5 (permalink)  
Old 11-02-03, 07:51 PM
evo4ever evo4ever is offline
Software Developer Guru
 
Join Date: Aug 2003
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, ive just banged up a quick script which works. Here goes:

PHP Code:

// Get the contents of the file as a string.

$fp fopen($chat_file"r");
$file fgets($fp);
fclose($fp);

/* Open the file again with an overwriting mode, search for the desired nickname and replace with nothing (or something else). Put the new content into the file, then close it.
*/

$fp fopen($chat_file"w");
$replacement ""// Or anything else.
$newContent str_replace($nickname$replacement$file);
if(
fputs($fp$newContent)) print "Replacement Done.";
fclose($fp); 
Reply With Quote
  #6 (permalink)  
Old 11-02-03, 08:15 PM
JJSmallballer JJSmallballer is offline
Newbie Coder
 
Join Date: Oct 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you! I am going to try it now, I'll check and see if it works!
Reply With Quote
  #7 (permalink)  
Old 11-02-03, 08:19 PM
JJSmallballer JJSmallballer is offline
Newbie Coder
 
Join Date: Oct 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Nah, it deleted everything... It didn't keep the people who were still on in the txt file...
Reply With Quote
  #8 (permalink)  
Old 11-02-03, 08:35 PM
evo4ever evo4ever is offline
Software Developer Guru
 
Join Date: Aug 2003
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Could I look at your txt file? There might be something in there which causing the script from working properly.
Reply With Quote
  #9 (permalink)  
Old 11-02-03, 09:36 PM
JJSmallballer JJSmallballer is offline
Newbie Coder
 
Join Date: Oct 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Uploaded the wrong file... I get this error with your script anyways:

[02-Nov-2003 22:33:30] PHP Warning: fgets(): supplied argument is not a valid stream resource in /home/jjs47z/public_html/newchat/logout.php on line 5
[02-Nov-2003 22:33:30] PHP Warning: fclose(): supplied argument is not a valid stream resource in /home/jjs47z/public_html/newchat/logout.php on line 6
[02-Nov-2003 22:33:30] PHP Warning: fputs(): supplied argument is not a valid stream resource in /home/jjs47z/public_html/newchat/logout.php on line 14
[02-Nov-2003 22:33:30] PHP Warning: fclose(): supplied argument is not a valid stream resource in /home/jjs47z/public_html/newchat/logout.php on line 15

BTW, the TXT file is just a normal notepad file, and it is chmodded to 777
__________________
Over 5 years in design. www.webatech.com www.tabscentral.com/temp/
Reply With Quote
  #10 (permalink)  
Old 11-03-03, 10:02 AM
evo4ever evo4ever is offline
Software Developer Guru
 
Join Date: Aug 2003
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Make sure the txt file is chmod 0777. Theres nothing wrong with my script, I don't get those errors on my server (running v4.3.3). Try changing the names of the $fp variables. You have two so name the first one $fp1 and the second one $fp2. If that doesn't work try unset()ing the first $fp, after the fclose().
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 07:23 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.