new2code Posted March 4, 2010 Share Posted March 4, 2010 Hi I have a php scrip that looks for a sring and removes everything else in a file just saving the whole line with that string.($key = "C " as you can see its actually repeating the same thing and filtering few stings. I was wondering is there a way I can make it ask for the string before it proceeds ans a user input? I know it might not even make much sence. I am very new for php or any programming language but learning. here is the code: <?php $key = "C "; // <--------- [b]to prompt to ask for this[/b] //load file into $fc array $fc=file("input.txt"); //open same file and use "w" to clear file $f=fopen("output.txt","w"); //loop through array using foreach foreach($fc as $line) { if (strstr($line,$key)) //look for $key in each line -adding ! if (!strstr($line,$key)) does the opposite it will delete everything else than A fputs($f,$line); //place $line back in file } fclose($f); $key2 = "C 0."; //load file into $fc array $fe=file("output.txt"); //open same file and use "w" to clear file $fi=fopen("final.txt","w"); //loop through array using foreach foreach($fe as $line2) { if (!strstr($line2,$key2)) //look for $key in each line -adding ! if (!strstr($line,$key)) does the opposite it will delete everything else than A fputs($fi,$line2); //place $line back in file } fclose($fi); $key3 = "F "; //filters just this text at the begining of a line //load file into $fc array $fc3=file("input.txt"); //open same file and use "w" to clear file $f3=fopen("output2.txt","w"); //loop through array using foreach foreach($fc3 as $line3) { if (strstr($line3,$key3)) //look for $key in each line -adding ! if (!strstr($line,$key)) does the opposite it will delete everything else than A fputs($f3,$line3); //place $line back in file } fclose($f3); $key4 = "F 0."; //load file into $fc array $fc4=file("output2.txt"); //open same file and use "w" to clear file $f4=fopen("final2.txt","w"); //loop through array using foreach foreach($fc4 as $line4) { if (!strstr($line4,$key4)) //look for $key in each line -adding ! if (!strstr($line,$key)) does the opposite it will delete everything else than A fputs($f4,$line4); //place $line back in file } fclose($f4); ?> Thank you so much in advance. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 4, 2010 Share Posted March 4, 2010 Changes made include the IF statement at the top, assignment of $key right after that, and an inclusion of a form at the borrom of the script. <?php if (isset($_POST['key'])) { $key = $_POST['key']; // <--------- [b]to prompt to ask for this[/b] //load file into $fc array $fc=file("input.txt"); //open same file and use "w" to clear file $f=fopen("output.txt","w"); //loop through array using foreach foreach($fc as $line) { if (strstr($line,$key)) //look for $key in each line -adding ! if (!strstr($line,$key)) does the opposite it will delete everything else than A fputs($f,$line); //place $line back in file } fclose($f); $key2 = "C 0."; //load file into $fc array $fe=file("output.txt"); //open same file and use "w" to clear file $fi=fopen("final.txt","w"); //loop through array using foreach foreach($fe as $line2) { if (!strstr($line2,$key2)) //look for $key in each line -adding ! if (!strstr($line,$key)) does the opposite it will delete everything else than A fputs($fi,$line2); //place $line back in file } fclose($fi); $key3 = "F "; //filters just this text at the begining of a line //load file into $fc array $fc3=file("input.txt"); //open same file and use "w" to clear file $f3=fopen("output2.txt","w"); //loop through array using foreach foreach($fc3 as $line3) { if (strstr($line3,$key3)) //look for $key in each line -adding ! if (!strstr($line,$key)) does the opposite it will delete everything else than A fputs($f3,$line3); //place $line back in file } fclose($f3); $key4 = "F 0."; //load file into $fc array $fc4=file("output2.txt"); //open same file and use "w" to clear file $f4=fopen("final2.txt","w"); //loop through array using foreach foreach($fc4 as $line4) { if (!strstr($line4,$key4)) //look for $key in each line -adding ! if (!strstr($line,$key)) does the opposite it will delete everything else than A fputs($f4,$line4); //place $line back in file } fclose($f4); } ?> <html> <head></head> <body> <form name="keyInput" action="" method="POST"> Enter Key: <input type="text" name="key"> <br /><br /> <button type="submit">Submit key</button> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 4, 2010 Share Posted March 4, 2010 After further review, that code is really inefficient. There is no reason to loop over every line multiple times. Let me see what I can do and I'll post back. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 4, 2010 Share Posted March 4, 2010 OK, I believe this should produce the same result as your original script with much, much less overhead. <?php if (isset($_POST['key1'])) { $key1 = $_POST['key1']; $key2 = "C 0."; $key3 = "F "; $key4 = "F 0."; //Load file into an array $fileLines = file("input.txt"); //Open same file and use "w" to clear file $fileObj = fopen("output.txt","w"); //loop through array using foreach foreach($fileLines as $line) { //Keep lines which contain $key1 & $key3, but do not contain $key2 or $key4 if (strpos($line, $key1) && !strpos($line, $key2) && strpos($line, $key3) && !strpos($line, $key4)) { fputs($fileObj, $line); //place $line back in file } } //Close the file handle fclose($fileObj); } ?> <html> <head></head> <body> <form name="keyInput" action="" method="POST"> Enter Key: <input type="text" name="key1"> <br /><br /> <button type="submit">Submit key</button> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
new2code Posted March 9, 2010 Author Share Posted March 9, 2010 mjdamato thank you so much. I'm so bad at this I created 2 file to try it out and finaly realized heeds one php with the html code in it. It works fine Thank you. you guys are awesome.! Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2010 Share Posted March 9, 2010 mjdamato thank you so much. I'm so bad at this I created 2 file to try it out and finaly realized heeds one php with the html code in it. What? Not at all. In fact I typically separate my logic in one file and the HTML in another file. Just put an include at the end of the PHP section with the file with the HTML code. 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.