Jump to content

Searching text files


rachelk

Recommended Posts

I'm trying to search a text file which has lines like this:

 

first-sequence

second-sequence

sequence-sequence

 

I only want to search for the first word of every line though, so if I search for "sequence" I only wanted the 3rd line to be outputted, although the first two have "sequence" in them it's not the first word.. I'm wondering if this is possible at all, or would it be too difficult?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/168246-searching-text-files/
Share on other sites

Thanks for the replies :)

 

I'm very much a newbie to this so I don't know the basic steps I should be taking to find a word.. I've tried your code Thorpe, thanks for that, but I'm getting this error:

Warning: fclose(): supplied argument is not a valid stream resource in (path) on line 10

 

 

I also have an existing search script that I thought I could modify to do this, would I be able to accomplish this by fiddling around with this line, which I think is the one controlling the finding of the word?

 

if(preg_match("/$word/i",$found)&&(!in_array($found, $file)))

 

Link to comment
https://forums.phpfreaks.com/topic/168246-searching-text-files/#findComment-888177
Share on other sites

Try this instead:

 

<?php
$f = fopen("file.txt", "r");
while ($scan = fscanf($f, "%s-%s\n")) {
  list ($item, $junk) = $scan;
  if ($item == 'sequence') {
    echo "found";
  }
}
fclose($f);
?>

 

The line of code you have below is PHP Regex.. not my specialty. Try posting in the regex forum. ;)

Link to comment
https://forums.phpfreaks.com/topic/168246-searching-text-files/#findComment-888183
Share on other sites

Sorry, thought it was obvious.

Not taking credit for his work, just fixing the typo.

 

I'm a tool, I'm a tool, I'm a tool.

;)

 

oh haha thats not what i ment. just always good to let the guy know how to fix the mistake next time, but checking all his variables are correct.

Link to comment
https://forums.phpfreaks.com/topic/168246-searching-text-files/#findComment-888612
Share on other sites

Thanks, I really appreciate the help.

 

I think this is just too advanced for me, I tried the edited code there but it just outputs an empty page, I think I'll just learn the basics a bit more and then attempt to do this because I'm getting nowhere and am not understanding why. Thanks again for the help.

Link to comment
https://forums.phpfreaks.com/topic/168246-searching-text-files/#findComment-888677
Share on other sites

$f = fopen("file.txt", "r");
while ($scan = fscanf($f, "%s-%s\n")) {
  list ($item, $junk) = $scan;
  if ($item == 'sequence') {
    echo "found";
  }
}
fclose($handle);

 

I was curious to know, the format requirement for the 2nd argument of the fscanf function that must follow the sprintf documentation, states that "s"  is treated as and presented as a string, does this include special characters, spaces, numeric values..etc?

Link to comment
https://forums.phpfreaks.com/topic/168246-searching-text-files/#findComment-888771
Share on other sites

$f = fopen("file.txt", "r");
while ($scan = fscanf($f, "%s-%s\n")) {
  list ($item, $junk) = $scan;
  if ($item == 'sequence') {
    echo "found";
  }
}
fclose($handle);

 

I also realised this is NOT the correct format to use. The array brings back word-word, instead of word,-,word

Link to comment
https://forums.phpfreaks.com/topic/168246-searching-text-files/#findComment-888775
Share on other sites

okay try this

 

<?php
$handle = fopen("file.txt","r");
if($handle){
while(!feof($handle)){
$words = fgets($handle,1024);
$word = explode("-",$words);
   if($word[0] == "sequence"){
      print "found!";
    }
  }
fclose($handle);
}
?>

 

apple-sequence
orange-sequence
sequence-sequence

Link to comment
https://forums.phpfreaks.com/topic/168246-searching-text-files/#findComment-888779
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.