boneXXX Posted April 23, 2007 Share Posted April 23, 2007 Could some one comment this codes what they do please? I found it on the web and I want to understand it. Thanks in advance. <?php $fname = "test.txt"; $exclude = "some string"; $lines = file($fname); $out = ""; foreach ($lines as $line) { if (strstr($line, $exclude) == "") { $out .= $line; } } $f = fopen($fname, "w"); fwrite($f, $out); fclose($f); ?> Quote Link to comment Share on other sites More sharing options...
mpharo Posted April 23, 2007 Share Posted April 23, 2007 Could some one comment this codes what they do please? I found it on the web and I want to understand it. Thanks in advance. <?php //sets the file you want to read $fname = "test.txt"; //excludes a string from the modification $exclude = "some string"; //puts the opened file into array $lines $lines = file($fname); //sets up $out as empty variable $out = ""; //sets up the loop foreach ($lines as $line) { //if it is equal to nothing after the excluded string $out equals the first part of the string if (strstr($line, $exclude) == "") { $out .= $line; } } //reopens the file with write permissions $f = fopen($fname, "w"); //writes the file with the changes that were made above fwrite($f, $out); //closes the file fclose($f); ?> Quote Link to comment Share on other sites More sharing options...
boneXXX Posted April 23, 2007 Author Share Posted April 23, 2007 Thanks a lot it is perfect. Quote Link to comment Share on other sites More sharing options...
boneXXX Posted April 23, 2007 Author Share Posted April 23, 2007 It is close to what I am looking for but not exactly. I am trying to find a specific line, which starts with a unique word, to delete in a .txt file then rewrite that line. Could you help me on this please ? I am ok till finding the line but don't know how to delete and rewrite it? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.