Jump to content

Translator help


supermeip
 Share

Recommended Posts

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

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

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

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

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

variableType(characterCount) "literal value"

search value:string(1) "a" compare value:string(2) "a "

"a" != "a " (notice the space)

Edited by MrFish
Link to comment
Share on other sites

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
it means it didn't find. see Mr. Fish's post.
Link to comment
Share on other sites

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...