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); ?> Link to comment https://forums.phpfreaks.com/topic/48294-commenting-php-codes/ 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); ?> Link to comment https://forums.phpfreaks.com/topic/48294-commenting-php-codes/#findComment-236085 Share on other sites More sharing options...
boneXXX Posted April 23, 2007 Author Share Posted April 23, 2007 Thanks a lot it is perfect. Link to comment https://forums.phpfreaks.com/topic/48294-commenting-php-codes/#findComment-236087 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? Link to comment https://forums.phpfreaks.com/topic/48294-commenting-php-codes/#findComment-236102 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.