Jump to content

Prompt for user input.


new2code

Recommended Posts

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.

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.