supermeip Posted September 20, 2010 Share Posted September 20, 2010 Can someone tell me how to make this with php:Take information from HTML text form via post methodAnd use the fgets function to find the text in a text file then print that and the next line?I tried:<?php$x=$_POST["find"];$txt=fopen("g.txt","r");while(!feof($txt)) { fgets($txt); if(fgets($txt)==$x) echo $x . "=" .fgets($txt) ; }fclose($txt);?>But I can't get it to work Link to comment Share on other sites More sharing options...
MrFish Posted September 20, 2010 Share Posted September 20, 2010 Here are the two problems I see:1. You probably aren't searching whole lines at a time. So using == would compare the post variable with each line of the file. So instead use strrpos to search each line for the post variable: <?php $find = $_POST["find"]; $file = fopen("g.txt","r"); while(!feof($file)) { $thisLine = fgets($file); $found = strrpos($thisLine, $find); if($found > -1) echo "$found was found in $thisLine"; } fclose($file);?> 2. When use use the function fgets, it gets the next line and removes the previous one. You program doesn't know that inside the while loop you only want the same line, so this is what it sees. <?php$x=$_POST["find"];$txt=fopen("g.txt","r");while(!feof($txt)) { fgets($txt); //Get the next line and remove the last if(fgets($txt)==$x) //Get the next line, remove the last, and compare it with $x echo $x . "=" .fgets($txt); //Get, yet again, the next line, remove the last, and concatenate it to this string }fclose($txt);?> So you aren't checking every line. Only every 2 lines. Link to comment Share on other sites More sharing options...
supermeip Posted September 20, 2010 Author Share Posted September 20, 2010 I actualy want it to read the next linesay g.txt is thisa10b20and someone enters a in the form I want it to return the 10 Link to comment Share on other sites More sharing options...
MrFish Posted September 20, 2010 Share Posted September 20, 2010 But you still are getting every 2 lines. So someone enters a, you should bet b. Link to comment Share on other sites More sharing options...
supermeip Posted September 21, 2010 Author Share Posted September 21, 2010 actualy it just reurns 0 instead of 10 Link to comment Share on other sites More sharing options...
supermeip Posted September 21, 2010 Author Share Posted September 21, 2010 it gives "0 was found in a" if i enter a with the text file stated above Link to comment Share on other sites More sharing options...
supermeip Posted September 21, 2010 Author Share Posted September 21, 2010 actualy it returns the above statement no matter if i enter a,b,or 10 Link to comment Share on other sites More sharing options...
justsomeguy Posted September 21, 2010 Share Posted September 21, 2010 It looks like you're just printing the return value from strrpos. That will return the position of the substring if it was found (note that it will never return -1, if it was not found it returns boolean false). A value of 0 means that it was found at the beginning of the line. Link to comment Share on other sites More sharing options...
supermeip Posted September 21, 2010 Author Share Posted September 21, 2010 how do i just get it to reurn the 10 if i enter a Link to comment Share on other sites More sharing options...
justsomeguy Posted September 21, 2010 Share Posted September 21, 2010 Your loop should read 2 lines at a time. One would be the line that contains what you're searching for, and one would be the value to return. If your search matches, then you return the value. That's really all there is to it, just read 2 lines, compare, and return if it matches.Check the manual to see how to use fgets correctly:http://www.php.net/manual/en/function.fgets.php Link to comment Share on other sites More sharing options...
supermeip Posted September 21, 2010 Author Share Posted September 21, 2010 so<?php$x=$_POST["find"];$txt=fopen("g.txt","r");while(!feof($txt)) { if(fgets($txt)==$x) echo $x . "=" .fgets($txt); }fclose($txt);?>should Work but it returns nothing is there a problem with me compairing the variable if so how do i do it right Link to comment Share on other sites More sharing options...
justsomeguy Posted September 21, 2010 Share Posted September 21, 2010 Did you print out the value to see exactly what you're comparing, or are you making assumptions about what the data is? <?php$x=$_POST["find"];$txt=fopen("g.txt","r");echo 'search value:<br>';var_dump($x);echo '<br>';while(!feof($txt)){ $search = fgets($txt); echo 'compare value:<br>'; var_dump($search); echo '<br>'; if($search==$x) echo $x . "=" .fgets($txt); }fclose($txt);?> Run that and see if the search value really matches the compare value. Link to comment Share on other sites More sharing options...
supermeip Posted September 21, 2010 Author Share Posted September 21, 2010 I get search value:string(1) "a" compare value:string(2) "a " compare value:string(3) "10 " compare value:string(2) "b " compare value:string(3) "20 " compare value:string(2) "c " compare value:string(2) "30" What's that mean Link to comment Share on other sites More sharing options...
MrFish Posted September 21, 2010 Share Posted September 21, 2010 variableType(characterCount) "literal value" search value:string(1) "a" compare value:string(2) "a " "a" != "a " (notice the space) Link to comment Share on other sites More sharing options...
thescientist Posted September 21, 2010 Share Posted September 21, 2010 I get search value:string(1) "a" compare value:string(2) "a " compare value:string(3) "10 " compare value:string(2) "b " compare value:string(3) "20 " compare value:string(2) "c " compare value:string(2) "30" What's that meanit means it didn't find. see Mr. Fish's post. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 21, 2010 Share Posted September 21, 2010 So it's searching for a string of length 1, and it's comparing with a string of length 2. What's the extra character on the end? Did you read the description about how fgets works? Link to comment Share on other sites More sharing options...
supermeip Posted September 21, 2010 Author Share Posted September 21, 2010 Is it the return char?so how do I get it to find "a " Link to comment Share on other sites More sharing options...
justsomeguy Posted September 21, 2010 Share Posted September 21, 2010 Is it the return char?I don't know, what does the fgets documentation say about the length parameter?Can you tell I'm trying to show you how to research these things?Why would you want it to search for something they didn't type in? Wouldn't you want to remove the extra characters from the line and then compare the rest? How about this:http://www.php.net/manual/en/function.trim.php Link to comment Share on other sites More sharing options...
supermeip Posted September 21, 2010 Author Share Posted September 21, 2010 Thanks for the help I got it to work<?php$x=$_POST["find"];$txt=fopen("g.txt","r");while(!feof($txt)) { if(trim(fgets($txt))==$x) echo $x . "=" .fgets($txt); }fclose($txt);?> Link to comment Share on other sites More sharing options...
justsomeguy Posted September 21, 2010 Share Posted September 21, 2010 Here's another version, it makes sense to trim the input also. Your version will also search every line until it finds it, meaning that it may look at the replacement, see a match, and print the next line. If you searched for 10, for example, your code would return "b". <?php$x=trim($_POST["find"]);$txt=fopen("g.txt","r");while(!feof($txt)){ $search = trim(fgets($txt)); $replace = trim(fgets($txt)); if($search==$x) echo $x . "=" . $replace;}fclose($txt);?> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.