adzie Posted July 30, 2009 Share Posted July 30, 2009 Hello folks, I'm trying to find a row in a text file, so far I think I have been able to get the script to establish a row exists but I've been unable to retreive that row. Any suggestions $file = file_get_contents("test.txt"); $searchstrings = 'home'; $breakstrings = explode(',',$searchstrings); foreach ($breakstrings as $values){ if(!strpos($file, $values)) { echo $values." string not found!\n"; } else { echo $values." string Found!\n"; } } The structure of the text file is the format below. 2009/07/30 Home 1 2 3 4 5 6 2009/07/31 Home 7 5 3 9 1 0 2009/08/01 Home 1 3 4 5 6 9 Link to comment https://forums.phpfreaks.com/topic/168209-php-find-information-in-text-file/ Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 check the "read file line by line" example here http://www.w3schools.com/PHP/php_file.asp each time you grab the line you can do a stristr() match to see if it contains "home" and then do whatever you want with the line Link to comment https://forums.phpfreaks.com/topic/168209-php-find-information-in-text-file/#findComment-887213 Share on other sites More sharing options...
abazoskib Posted July 30, 2009 Share Posted July 30, 2009 Hello folks, I'm trying to find a row in a text file, so far I think I have been able to get the script to establish a row exists but I've been unable to retreive that row. Any suggestions $file = file_get_contents("test.txt"); $searchstrings = 'home'; $breakstrings = explode(',',$searchstrings); You are exploding $searchStrings which is equal to 'home'. I think you meant to do explode($searchstrings,$file); right? Link to comment https://forums.phpfreaks.com/topic/168209-php-find-information-in-text-file/#findComment-887242 Share on other sites More sharing options...
adzie Posted July 31, 2009 Author Share Posted July 31, 2009 I changed my line as abazoskib suggested and the whole file prints to screen, any thoughts Link to comment https://forums.phpfreaks.com/topic/168209-php-find-information-in-text-file/#findComment-887477 Share on other sites More sharing options...
lonewolf217 Posted July 31, 2009 Share Posted July 31, 2009 did you try my idea ? Link to comment https://forums.phpfreaks.com/topic/168209-php-find-information-in-text-file/#findComment-887515 Share on other sites More sharing options...
adzie Posted July 31, 2009 Author Share Posted July 31, 2009 Hi Lone Wolf, I tried interpeting your idea, again similar result. I'm sure its me. <?php $file = fopen("test.txt", "r") or exit("Unable to open file!"); $string = 'home'; if(stristr($string, 'home') == TRUE) { echo 'Record Found'; } fclose($file); ?> Link to comment https://forums.phpfreaks.com/topic/168209-php-find-information-in-text-file/#findComment-887541 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.