wappy 0 Report post Posted July 7, 2006 Say i have a file file.txt... Can someone give me an example of how to delete a string from this file ie..The file contains:wappy::sucks::at::php--- i want to delete sucks:: Share this post Link to post Share on other sites
ijin 0 Report post Posted July 7, 2006 Well, you can't really do much but do a str_replace() on the file.. so here's how it would look like: $data = file("file.txt"); // puts whole file into array$file = file("file.txt", "w"); // We'll overwriteforeach($data as $value) { //loop through each linestr_replace("sucks::", "love::", $value); // or "" instead of love :)fputs($file, $value); // write into the file}fclose($file); And that would be one approach ;-) You can also use fopen to read the file, use regexp and probably a myriad of other things. Share this post Link to post Share on other sites
peroim 0 Report post Posted July 12, 2006 How about reading the file, make it explode at '::' and then add the pieces that don't contain 'sucks'? The code would look like this: <PHP$newcontent = ""; //Just setting a variable$file = ("yourfile.txt"); //This is your file$opened = fopen($file, "r"); //This is where we open the file to read from it$filecontent = fread($opened, filesize($file)); //Now, we're reading the fileclose($opened); //And closing$filecontent = explode("::", $filecontent); //Now the exploding of the variableforeach($filecontent => $value){ //A loop that will run once for each piece of the array (I'm not really sure of this syntaxif($value != "sucks"){ //If the piece is NOT "sucks", we continue$newcontent = $newcontent.$value."::"; //Adding the piece + "::" to a variable}}$opened = fopen($file, "w"); //Now open the file again to writefwrite($opened, $newcontent); //Put the data in the fileclose($opened); //Close the file and we're done?> I didn't test the code but I think it would work Share this post Link to post Share on other sites