Looktrne Posted March 10, 2012 Share Posted March 10, 2012 I have a large text file that I need to search and extract text from. I have some code that somewhat works but is not good for what I need because it only reads one line at a time. I need to be able to echo all code between two strings and continue scanning the entire document. I am attaching the TXT file that is being read by the script: Here is the script: <? $searchthis = "Problem:"; $search="Check:"; $matches = array(); $handle = @fopen("1numbers.txt", "r")or die("can't open file"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); if(strpos($buffer, $searchthis) !== FALSE) echo "<br>". $buffer."<br>"; if(strpos($buffer, $search) !== FALSE) echo "<br>". $buffer."<br>"; } fclose($handle); } ?> you can see what this script outputs by visiting this link: http://yourautofix.com/data/data.php but my problem is it only outputs one line of text that finds the search match. I need it to output all lines of text between two matches for example any text between "Problem:" and "Check:" should be Echo'd and any text between "Check:" and "Likely:" should be echo'd there may be 1 line or 20 lines of text between the tags... I need to print all lines between the 2 determined search strings and then continue through the text file displaying all matches between the search strings in a large file. any thoughts on how I can get this done or point me in the right direction? Thanks for any input on this Paul 17752_.txt Quote Link to comment https://forums.phpfreaks.com/topic/258658-need-help-please-searching-and-extracting-from-text-file/ Share on other sites More sharing options...
redsmurph Posted March 10, 2012 Share Posted March 10, 2012 After a search hit you need to set a state variable that you then test for to see if further lines should be output for each loop. This variable should be cleared when you reach the end of a group of lines (the next Check or Likely?). While the state variable is set you echo all lines. You might run into the problem of reading too much, but the above is principally what you need to do. Quote Link to comment https://forums.phpfreaks.com/topic/258658-need-help-please-searching-and-extracting-from-text-file/#findComment-1325921 Share on other sites More sharing options...
Looktrne Posted March 10, 2012 Author Share Posted March 10, 2012 Red do you have an example code I can see so I know what I am trying to do? thanks Paul Quote Link to comment https://forums.phpfreaks.com/topic/258658-need-help-please-searching-and-extracting-from-text-file/#findComment-1325923 Share on other sites More sharing options...
redsmurph Posted March 10, 2012 Share Posted March 10, 2012 The principle: $echo = false; while (!feof($handle)) { $buffer = fgets($handle); if (found start pattern) $echo = true; if (found end pattern or a new start pattern?) $echo = false; if ($echo) echo $buffer; } You know better than me what determines the end of the sequence. Cheers, Anders Quote Link to comment https://forums.phpfreaks.com/topic/258658-need-help-please-searching-and-extracting-from-text-file/#findComment-1325933 Share on other sites More sharing options...
litebearer Posted March 10, 2012 Share Posted March 10, 2012 1. do you have a larger chunk of the text file with which we can 'play'? 2. how is the text file created/derived? Quote Link to comment https://forums.phpfreaks.com/topic/258658-need-help-please-searching-and-extracting-from-text-file/#findComment-1325947 Share on other sites More sharing options...
Looktrne Posted March 11, 2012 Author Share Posted March 11, 2012 Thanks Red this looks like a simple concept I will play around with it tomorrow and see how it goes Paul Quote Link to comment https://forums.phpfreaks.com/topic/258658-need-help-please-searching-and-extracting-from-text-file/#findComment-1326048 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.