adamh91 Posted October 25, 2006 Share Posted October 25, 2006 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.[code]$handle = fopen("usernames.txt","r+");while (!feof($handle)) {$data = fgets($handle); }$data=str_replace($_GET["User"],"",$data);fwrite($handle, $Data);fclose($handle);[/code]thanks ;D Link to comment https://forums.phpfreaks.com/topic/25084-cant-remove-string-from-file/ Share on other sites More sharing options...
kenrbnsn Posted October 25, 2006 Share Posted October 25, 2006 What is the format of the file?Ken Link to comment https://forums.phpfreaks.com/topic/25084-cant-remove-string-from-file/#findComment-114361 Share on other sites More sharing options...
adamh91 Posted October 25, 2006 Author Share Posted October 25, 2006 fopen("[b]usernames.txt[/b]","r+");cant you read ;).txt file :P Link to comment https://forums.phpfreaks.com/topic/25084-cant-remove-string-from-file/#findComment-114366 Share on other sites More sharing options...
chiilas Posted October 25, 2006 Share Posted October 25, 2006 are you sure you set the right chmod? Link to comment https://forums.phpfreaks.com/topic/25084-cant-remove-string-from-file/#findComment-114399 Share on other sites More sharing options...
Barand Posted October 25, 2006 Share Posted October 25, 2006 Now there's a text-book example of how to drive away good help. Link to comment https://forums.phpfreaks.com/topic/25084-cant-remove-string-from-file/#findComment-114404 Share on other sites More sharing options...
adamh91 Posted October 25, 2006 Author Share Posted October 25, 2006 [quote author=Barand link=topic=112700.msg457594#msg457594 date=1161805113]Now there's a text-book example of how to drive away good help.[/quote]Sorry, Was just a joke :( Link to comment https://forums.phpfreaks.com/topic/25084-cant-remove-string-from-file/#findComment-114414 Share on other sites More sharing options...
gmwebs Posted October 25, 2006 Share Posted October 25, 2006 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 [b][i]read[/i][/b] [url=http://uk2.php.net/manual/en/ref.filesystem.php]PHP Filesystem Reference[/url]. (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. Link to comment https://forums.phpfreaks.com/topic/25084-cant-remove-string-from-file/#findComment-114430 Share on other sites More sharing options...
adamh91 Posted October 25, 2006 Author Share Posted October 25, 2006 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 :Sany help?, thanks :D Link to comment https://forums.phpfreaks.com/topic/25084-cant-remove-string-from-file/#findComment-114467 Share on other sites More sharing options...
gmwebs Posted October 25, 2006 Share Posted October 25, 2006 Well that is the problem with using unstructured files.Have another look at [url=http://http://uk2.php.net/manual/en/function.file-put-contents.php]file_put_contents[/url] it should accomplish what you need. Provided of course you are using PHP version 5?Something along the lines of...[code]<?phpfile_put_contents($filename, $newdata, LOCK_EX);?>[/code] Link to comment https://forums.phpfreaks.com/topic/25084-cant-remove-string-from-file/#findComment-114475 Share on other sites More sharing options...
adamh91 Posted October 25, 2006 Author Share Posted October 25, 2006 PHP version 4.4.1 :-\any other ideas? Link to comment https://forums.phpfreaks.com/topic/25084-cant-remove-string-from-file/#findComment-114486 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.