Page 1 of 2

How do I search for text in a file?

Posted: Sun Aug 17, 2003 8:43 pm
by nick2
I need to search for txt in a file and grab that word and echo it.. is it possible?

Posted: Sun Aug 17, 2003 8:55 pm
by nigma

Code: Select all

define(FILENAME, "filename.txt");

$fp = fopen(FILENAME, "r") or die ("Failed to open " . FILENAME);
while (!feof($fp))
{
  $line = fgets($fp, 1024);
  if (ereg("SEARCHSTRING", $line, $results))
  {
    print $resultsї0];
  }
}
Try that, get back to me with results?

Posted: Sun Aug 17, 2003 9:02 pm
by nick2
Warning: fopen(): Unable to access testtxt in /home/virtual/site9/fst/var/www/html/tradingsystem/searchtest.PHP on line 2

Warning: fopen(testtxt): failed to open stream: No such file or directory in /home/virtual/site9/fst/var/www/html/tradingsystem/searchtest.PHP on line 2
Failed to open testtxt

Posted: Sun Aug 17, 2003 9:04 pm
by nigma
Oops, sorry, I had defined the filename and such earlier in php code.

I am revising my past reply right now.

Posted: Sun Aug 17, 2003 9:11 pm
by nick2
lol ok.. no errors.. but it doens't display anything.


Int he txt file I put 1024.. isn't that what it searches for?

Posted: Sun Aug 17, 2003 9:12 pm
by nigma
Did you change the define line to define FILENAME as the file you want to search?

Posted: Sun Aug 17, 2003 9:13 pm
by m3rajk
1024 is the number of bytes to read a a "line"

Posted: Sun Aug 17, 2003 9:14 pm
by nigma
I will just show you the entire script that I made (that also worked) and you can fiddle with it:

Code: Select all

<?php
define(FILENAME, "test.txt");

if (file_exists(FILENAME))
&#123;
  $fp = fopen(FILENAME, "r") or die ("Failed to open " . FILENAME);
  while (!feof($fp))
  &#123;
    $line = fgets($fp, 1024);
    if (ereg("test", $line, $results))
    &#123;
      print $results&#1111;0];
    &#125;
  &#125;
&#125;
else
&#123;
  print FILENAME . " could not be opened.";
&#125;
?>

Posted: Sun Aug 17, 2003 9:18 pm
by nick2
Thanks bud, have any idea how to make it similar to .ini file? Like it searches for Name then besides that is the word Nick?

like:

Name=Nick

:-/

please help ^_^

Posted: Sun Aug 17, 2003 9:22 pm
by nigma
You could do it using regular expressions. But you would have to know the format of the file that you where getting the data from.

Posted: Sun Aug 17, 2003 9:23 pm
by nick2
nigma use english.. i'm a noob ;)

I would be using .txt file.

Posted: Sun Aug 17, 2003 9:35 pm
by nigma
But how would the txt file be layed out?

Example #1:
Name: Don

Example #2:
Name=Don

That is what I mean? And I am assuming when you said use english you were not refering to the regular expression part right?

Posted: Sun Aug 17, 2003 9:40 pm
by nick2
lol ok. I'll be using Name=Nick

I would make it find Name.. but how would I make it find The value "nick" because you will be able to change it. so you can't search for the word "nick"

Posted: Sun Aug 17, 2003 9:53 pm
by nigma
In that case do this:

Code: Select all

if (preg_match("/Name=.*/", $line, $results))
&#123;
  print $results&#1111;0];
&#125;
Instead of this:

Code: Select all

if (ereg("test", $line, $results)) 
&#123; 
  print $results&#1111;0]; 
&#125;

Posted: Sun Aug 17, 2003 10:04 pm
by nick2
I know i'm asking lots of questions.. and i'm greatful.. but how would I make it just display the value.. not the whole thing: Name=Nivex

Just Nivex.


Thanks mate ^^