Archimedees Posted September 19, 2007 Share Posted September 19, 2007 I'm PHP newbie and need help with correction or logic to have this script work. I open a config file, put contents in array $arLines then walk through the array line by line using foreach $arLines $key => $value. I want to unset the lines in $arLines for which $_GET("Line"] variables passed from a form exist ie on the display a delete link has been activated. Thank you in advance. <?php //print_r($_GET); //line to delete if (isset($_GET["Line"])) { $nLine = $_GET["Line"]; }//end if (isset($_GET["Line"])) else { die(); }//end else (isset($_GET["Line"])) $strFilename = "page.conf"; $nFileHandle = fopen($strFilename, "r+"); //read all lines into array ("file"-command) // delete the line we want while (!feof($nFileHandle)) { $arLines = file($nFileHandle); foreach ($arLines as $Key => $value) { if(isset($nLine[])) { unset($key,$value); }// end of if(!$arLines["delete"]==false) }// end of foreach ($arLines as $Key => $value) }// end of while (!feof($nFileHandle)) ?> Quote Link to comment https://forums.phpfreaks.com/topic/69872-deleting-lines-in-a-file/ Share on other sites More sharing options...
Archimedees Posted September 19, 2007 Author Share Posted September 19, 2007 Please somebody??? Quote Link to comment https://forums.phpfreaks.com/topic/69872-deleting-lines-in-a-file/#findComment-350978 Share on other sites More sharing options...
Barand Posted September 19, 2007 Share Posted September 19, 2007 You don't need the fopen() and the while loop. Should be something like this (although I'm not too sure what the condition for deleting should be from your description) <?php //print_r($_GET); //line to delete if (isset($_GET["Line"])) { $nLine = $_GET["Line"]; }//end if (isset($_GET["Line"])) else { die(); }//end else (isset($_GET["Line"])) $strFilename = "page.conf"; //read all lines into array ("file"-command) // delete the line we want $arLines = file($strFilename ); foreach ($arLines as $Key => $value) { if(trim($value)==$nline) // or whatever the condition is { unset($arLines[$Key]); }// end of if(!$arLines["delete"]==false) }// end of foreach ($arLines as $Key => $value) ?> If deletions are to be permanent you now need to re-write the file. Quote Link to comment https://forums.phpfreaks.com/topic/69872-deleting-lines-in-a-file/#findComment-350985 Share on other sites More sharing options...
hemlata Posted September 19, 2007 Share Posted September 19, 2007 Hello, One more solution could be.. $arLines = file("page.conf"); if (isset($_GET["Line"])) { $result = array_search($_GET["Line"], $arLines); if (null != $result) { unset($arLines[$result]); } } Regards, Quote Link to comment https://forums.phpfreaks.com/topic/69872-deleting-lines-in-a-file/#findComment-351002 Share on other sites More sharing options...
Barand Posted September 19, 2007 Share Posted September 19, 2007 I think you'll need if (false !== $result) in case the match is on the first line. Quote Link to comment https://forums.phpfreaks.com/topic/69872-deleting-lines-in-a-file/#findComment-351009 Share on other sites More sharing options...
Archimedees Posted September 19, 2007 Author Share Posted September 19, 2007 Thank you Barand and Hemlata. Still got a problem. This script wipes out entire file :'( Any idea why? Please pardon my ignorance. <?php //print_r($_GET); //line to delete if (isset($_GET["Line"])) { $nLine = $_GET["Line"]; }//end if (isset($_GET["Line"])) else { die(); }//end else (isset($_GET["Line"])) $strFilename = "page.conf"; $arLines = file($strFilename); foreach ($arLines as $key => $value) { $result = array_search($nLine, $arLines); if (false !== $result) { unset($arLines[$result]); }// end of f (false !== $result) }// end of foreach ($arLines as $key => $value) //write new page.conf $nFileHandle = fopen("page.conf" "w"); $i=0; $nMaxlength = count($arLines); for ($i=0; $i<$nMaxlength; $i++) { $arCurrentLine = $arLines[$i]; $strNewLine =""; // add URL to string $strNewLine = $strNewLine.$arLines["url"]."\t"; $strNewLine = $strNewLine.$arLines["email"]. "\t"; $strNewLine = $strNewLine.$arLines["size"]. "\t"; $strNewLine = $strNewLine.$arLines["command"]. "\r\n"; fwrite($nFileHandle,$strNewLine); }//end for ($i=0; $i<$nMaxlength; $i++) fclose($nFileHandle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/69872-deleting-lines-in-a-file/#findComment-351140 Share on other sites More sharing options...
hemlata Posted September 24, 2007 Share Posted September 24, 2007 Hello, In the following code block, you are actually replacing value all the time instead of concatenating it. $strNewLine = $strNewLine.$arLines["url"]."\t"; $strNewLine = $strNewLine.$arLines["email"]. "\t"; $strNewLine = $strNewLine.$arLines["size"]. "\t"; $strNewLine = $strNewLine.$arLines["command"]. "\r\n"; use the below code and see whether this solves your problem or not $strNewLine = $strNewLine.$arLines["url"]."\t"; $strNewLine .= $strNewLine.$arLines["email"]. "\t"; $strNewLine .= $strNewLine.$arLines["size"]. "\t"; $strNewLine .= $strNewLine.$arLines["command"]. "\r\n"; Regards, Quote Link to comment https://forums.phpfreaks.com/topic/69872-deleting-lines-in-a-file/#findComment-353892 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.