How do I search for text in a file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

How do I search for text in a file?

Post by nick2 »

I need to search for txt in a file and grab that word and echo it.. is it possible?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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?
Last edited by nigma on Sun Aug 17, 2003 9:05 pm, edited 1 time in total.
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Oops, sorry, I had defined the filename and such earlier in php code.

I am revising my past reply right now.
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post 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?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Did you change the define line to define FILENAME as the file you want to search?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

1024 is the number of bytes to read a a "line"
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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;
?>
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post 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 ^_^
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

nigma use english.. i'm a noob ;)

I would be using .txt file.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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?
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post 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"
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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;
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post 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 ^^
Post Reply