Jump to content


Photo

Cant remove string from file ?


  • Please log in to reply
9 replies to this topic

#1 adamh91

adamh91
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 25 October 2006 - 06:19 PM

Hey, im trying to remove a string form a file, i tried using replace and replacing it with "", this dosen't work, ive set the chmod to the correct settings aswell, i cant see whats wrong with this.
$handle = fopen("usernames.txt","r+");
while (!feof($handle))
    {
$data = fgets($handle);
	}
$data=str_replace($_GET["User"],"",$data);
fwrite($handle, $Data);
fclose($handle);
thanks  ;D

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 October 2006 - 06:37 PM

What is the format of the file?

Ken

#3 adamh91

adamh91
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 25 October 2006 - 06:46 PM

fopen("usernames.txt","r+");
cant you read  ;)
.txt file  :P

#4 chiilas

chiilas
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 25 October 2006 - 07:32 PM

are you sure you set the right chmod?

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 25 October 2006 - 07:38 PM

Now there's a text-book example of how to drive away good help.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 adamh91

adamh91
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 25 October 2006 - 07:54 PM

Now there's a text-book example of how to drive away good help.

Sorry, Was just a joke  :(

#7 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 25 October 2006 - 08:12 PM

fgets() will return *just* a line. You need fread() or better yet, file_get_contents() as it has much better performance. You will then need to decide how to update the text file with your new data.

I suggest you read PHP Filesystem Reference. (see what I did there? ;))

For future reference, always try to debug your variables to see if they are what you expect. Had you echo'ed the output of $data in your script, you would have seen it return just one line.

#8 adamh91

adamh91
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 25 October 2006 - 09:47 PM

Thanks, i echoed the variable to test and it shows it without the string i want to replace, which is good.
but how can i write the new data to the file, i tried fwrite and file_put_contents, neither of them work, file_put says function unknown :S
any help?, thanks :D

#9 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 25 October 2006 - 10:00 PM

Well that is the problem with using unstructured files.

Have another look at file_put_contents it should accomplish what you need. Provided of course you are using PHP version 5?

Something along the lines of...
<?php

file_put_contents($filename, $newdata, LOCK_EX);

?>


#10 adamh91

adamh91
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 25 October 2006 - 10:32 PM

PHP version  4.4.1  :-\
any other ideas?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users