Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading files

Status
Not open for further replies.

varyaner

Programmer
Joined
Jul 24, 2003
Messages
5
Location
CH
Hi there,

I'm new to PHP. My question is the following. I wrote a script where the user needs to enter a string into a form, then the script reads from a txt file and compares it to what the user has written. the problem is, that it doesn't work properly, since, i assume, when there is more than one line in the text file, there is an invisible "code" added when reading the first line. i guess, that sounds weird, can't explain it any better. here's the script:
<?
$file=fopen(&quot;textfile.txt&quot;,&quot;r&quot;);
$string=fgets($file,1000);
fclose($file);
if ($string==$e_string) echo ok;
else echo bad;
?>

e_sting is the string the user entered.

in textfile.txt there would be something like:
hello
world
 
Assuming your text file is fairly small, I wouldn't bother with the fopen, gets, fclose commands. I would just use the file command, to read each line of the file into an array.

the invisible character you're referring to is likely a line break or a newline character.

something similar to this...
Code:
<?php
  $found = false;
  $lines = file(&quot;textfile.txt&quot;);
  foreach ($lines as $line) {
    str_replace(&quot;\n&quot;, &quot;&quot;, $line);
    str_replace(&quot;\r&quot;, &quot;&quot;, $line);
    if ($string == $line) {
      $found = true;
    }
  }
  if ($found) {
    echo &quot;good&quot;;
  } else {
    echo &quot;bad&quot;;
  }
?>
There are places to neaten that up, specifically the replacing of the newlines, but I figured this illustrates each step the most clearly.

Enjoy,
Rob
 
even if the text file has 1 single line, it attaches a \n no the end. so if u have multiple lines and possible matches, u'll have to make a loop, smthing like:

<?
$file=fopen(&quot;textfile.txt&quot;,&quot;r&quot;);
$string=fgets($file,1000);
fclose($file);

$possible = explode(&quot;\n&quot;, $string); // separate possible ones in an array
$i = 0; // start checking in the beginning of the array

while ($i <= count($possible)-1) { // -1 because all text files store an empty line in the end
if ($possible[$i] == $e_string) { $found = 1; break; }
}

if ($found == 1) { echo &quot;ok&quot;; } else { echo &quot;bad&quot;; }
?>

think that'll do it :P
 
@ jamesp0tter, what exactly does explode() do?
 
hmm, it still doesn't work...
 
which one still doesn't work? and how're you testing it?



if you're testing mine it's because I have a rather large oversight...

Code:
    str_replace(&quot;\n&quot;, &quot;&quot;, $line);
    str_replace(&quot;\r&quot;, &quot;&quot;, $line);
should read
    $line = str_replace(&quot;\n&quot;, &quot;&quot;, $line);
    $line = str_replace(&quot;\r&quot;, &quot;&quot;, $line);

and explode turns a string into an array, creating a new element every time it hits the specified character... as described here...


-Rob
 
ok, thanx rob, i found annother way anyways, if I use
chop($string), it works :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top