rachelk Posted July 31, 2009 Share Posted July 31, 2009 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 More sharing options...
phpSensei Posted July 31, 2009 Share Posted July 31, 2009 Open the file using the FGETS function http://php.about.com/od/phpfunctions/g/fgets_php.htm if you find the line you want, then explode '-', and get the first word from the array Link to comment https://forums.phpfreaks.com/topic/168246-searching-text-files/#findComment-887435 Share on other sites More sharing options...
trq Posted July 31, 2009 Share Posted July 31, 2009 $f = fopen("file.txt", "r"); while ($scan = fscanf($f, "%s-%s\n")) { list ($item, $junk) = $scan; if ($item == 'sequence') { echo "found"; } } fclose($handle); Link to comment https://forums.phpfreaks.com/topic/168246-searching-text-files/#findComment-887438 Share on other sites More sharing options...
rachelk Posted August 1, 2009 Author Share Posted August 1, 2009 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 More sharing options...
bundyxc Posted August 1, 2009 Share Posted August 1, 2009 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 More sharing options...
phpSensei Posted August 1, 2009 Share Posted August 1, 2009 ^^^ Should explain it was just a typo by Thorpe. He made a reasonable mistake, just forgot to close the $f, instead he put $handle Link to comment https://forums.phpfreaks.com/topic/168246-searching-text-files/#findComment-888204 Share on other sites More sharing options...
bundyxc Posted August 1, 2009 Share Posted August 1, 2009 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. Link to comment https://forums.phpfreaks.com/topic/168246-searching-text-files/#findComment-888207 Share on other sites More sharing options...
phpSensei Posted August 2, 2009 Share Posted August 2, 2009 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 More sharing options...
rachelk Posted August 2, 2009 Author Share Posted August 2, 2009 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 More sharing options...
phpSensei Posted August 2, 2009 Share Posted August 2, 2009 $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 More sharing options...
phpSensei Posted August 2, 2009 Share Posted August 2, 2009 $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 More sharing options...
phpSensei Posted August 2, 2009 Share Posted August 2, 2009 another thing!!! lol - (Left-justifies the variable value) that sign cant be used!!! Link to comment https://forums.phpfreaks.com/topic/168246-searching-text-files/#findComment-888777 Share on other sites More sharing options...
phpSensei Posted August 2, 2009 Share Posted August 2, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.