nicob Posted November 16, 2008 Share Posted November 16, 2008 I was wondering if it's possible to search in txt files and return the right lines only. Content of data.txt: one two three four ... When I search for 'three' I get: three I know there are a lot of search engines scripts, but they work with mysql. So I'm talking about a 'no mysql database required'-script. Ayone? Link to comment https://forums.phpfreaks.com/topic/132925-solved-possible-search-in-txt-files-and-return-related-lines-only/ Share on other sites More sharing options...
bobbinsbro Posted November 16, 2008 Share Posted November 16, 2008 use fgets() in a loop to get each line from the file separately, and use string comaring/regex functions to find the text you want on each line. add a condition to display/save only the lines that meat certain conditions. Link to comment https://forums.phpfreaks.com/topic/132925-solved-possible-search-in-txt-files-and-return-related-lines-only/#findComment-691220 Share on other sites More sharing options...
wildteen88 Posted November 16, 2008 Share Posted November 16, 2008 Have a look through the following topic. Its a flat file search script I created for a user a while back. Link to comment https://forums.phpfreaks.com/topic/132925-solved-possible-search-in-txt-files-and-return-related-lines-only/#findComment-691222 Share on other sites More sharing options...
bobbinsbro Posted November 16, 2008 Share Posted November 16, 2008 @wildteen: an error has occurred. The topic or board i am looking for appears to be either missing or off limits to me. Link to comment https://forums.phpfreaks.com/topic/132925-solved-possible-search-in-txt-files-and-return-related-lines-only/#findComment-691224 Share on other sites More sharing options...
nicob Posted November 16, 2008 Author Share Posted November 16, 2008 That topic is not accessible. :'( I found also another topic about that script => http://www.phpfreaks.com/forums/index.php?topic=205499.0 @bobbinsbro thanks for the tip. It sounds complex, but I will try something out Link to comment https://forums.phpfreaks.com/topic/132925-solved-possible-search-in-txt-files-and-return-related-lines-only/#findComment-691229 Share on other sites More sharing options...
bobbinsbro Posted November 16, 2008 Share Posted November 16, 2008 it shouldn't be that bad: <?php $fh = fopen("theFile.txt", r); //open the text file for reading while ($line = fgets($fh)){ //get each line from the file as $line //search for a value in the line here with regex or strstr or substr or strcmp if (//condition to check if search was successful){ echo $line; //print the line to screen $someVarOrArray = $line; //or you can save the contents of the line to a variable/array for later use. } } ?> Link to comment https://forums.phpfreaks.com/topic/132925-solved-possible-search-in-txt-files-and-return-related-lines-only/#findComment-691232 Share on other sites More sharing options...
nicob Posted November 16, 2008 Author Share Posted November 16, 2008 thanks for the example, bobbinsbro! Link to comment https://forums.phpfreaks.com/topic/132925-solved-possible-search-in-txt-files-and-return-related-lines-only/#findComment-691236 Share on other sites More sharing options...
bobbinsbro Posted November 16, 2008 Share Posted November 16, 2008 no problem. btw, i made a small (yet fatal) mistake in the code: $fh = fopen("theFile.txt", r); should be: $fh = fopen("theFile.txt", "r"); Link to comment https://forums.phpfreaks.com/topic/132925-solved-possible-search-in-txt-files-and-return-related-lines-only/#findComment-691238 Share on other sites More sharing options...
wildteen88 Posted November 16, 2008 Share Posted November 16, 2008 @wildteen: an error has occurred. The topic or board i am looking for appears to be either missing or off limits to me. That thread should now be available. Link to comment https://forums.phpfreaks.com/topic/132925-solved-possible-search-in-txt-files-and-return-related-lines-only/#findComment-691266 Share on other sites More sharing options...
nicob Posted November 16, 2008 Author Share Posted November 16, 2008 Thank you, wildteen88! a very good script. question: Can you also link directly to a result? ex. http://www.domain.com/search.php?q=keyword @bobbinsbro I have noticed that you forgot the "". But it's about the scenario you described. Link to comment https://forums.phpfreaks.com/topic/132925-solved-possible-search-in-txt-files-and-return-related-lines-only/#findComment-691404 Share on other sites More sharing options...
wildteen88 Posted November 16, 2008 Share Posted November 16, 2008 Thank you, wildteen88! a very good script. question: Can you also link directly to a result? ex. http://www.domain.com/search.php?q=keyword Yes that is possible. You have two options. 1. change all instances of $_POST to $_GET and change the forms submit method to get 2. Or just change all instances $_POST to $_REQUEST leaving the form intact. Now you can perform searches from both your form and url. The script is capable of searching for more than one keyword, have a look in page two of the thread. Link to comment https://forums.phpfreaks.com/topic/132925-solved-possible-search-in-txt-files-and-return-related-lines-only/#findComment-691410 Share on other sites More sharing options...
nicob Posted November 16, 2008 Author Share Posted November 16, 2008 Perfect! Link to comment https://forums.phpfreaks.com/topic/132925-solved-possible-search-in-txt-files-and-return-related-lines-only/#findComment-691417 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.