Jump to content
xisto Community
Sign in to follow this  
wappy

File String Delete?

Recommended Posts

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.