stonebergftw Posted July 4, 2011 Share Posted July 4, 2011 I have a problem that I don't know what to call. It's simple I think. I have file.txt . In this file there are a lot of entries,some of which contain the string Goober######", where # is 1-infinite random character/s. This string always ends with the character, ". I need to go through file.txt and write each occurrence to an array. Can someone help me with the syntax for this? Quote Link to comment https://forums.phpfreaks.com/topic/241096-reading-through-file/ Share on other sites More sharing options...
stonebergftw Posted July 4, 2011 Author Share Posted July 4, 2011 I solved this mysql by using preg_match. However, I'm still trying to figure out how to get it to find every occurrence, as it seems to be stopping after the first. Quote Link to comment https://forums.phpfreaks.com/topic/241096-reading-through-file/#findComment-1238363 Share on other sites More sharing options...
jobbe Posted July 4, 2011 Share Posted July 4, 2011 Asuming the file looks something like this: Goobergree098ge123" Goober4gregregr56" Goober78csgpouw59" Goober0jl345312" Goober34agro9szduvæl45" Goober901gwradaegre" Goober6jfiaoæryf89378" then i would try: <?PHP $content = file('file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach($content as $id => $line){ $tmp = explode('Goober',$line); $data[$id] = explode('"', $tmp[1], -1); } var_dump($data); ?> which outputs an array Quote Link to comment https://forums.phpfreaks.com/topic/241096-reading-through-file/#findComment-1238365 Share on other sites More sharing options...
stonebergftw Posted July 4, 2011 Author Share Posted July 4, 2011 This definitely works. Now I'll just write code to pull only the data I need. Thank you sir. Quote Link to comment https://forums.phpfreaks.com/topic/241096-reading-through-file/#findComment-1238372 Share on other sites More sharing options...
jobbe Posted July 4, 2011 Share Posted July 4, 2011 This definitely works. Now I'll just write code to pull only the data I need. Thank you sir. You're welcome Quote Link to comment https://forums.phpfreaks.com/topic/241096-reading-through-file/#findComment-1238374 Share on other sites More sharing options...
stonebergftw Posted July 5, 2011 Author Share Posted July 5, 2011 Can you explain what the following line is doing? $data[$id] = explode('"', $tmp[1], -1); Quote Link to comment https://forums.phpfreaks.com/topic/241096-reading-through-file/#findComment-1238410 Share on other sites More sharing options...
aabid Posted July 5, 2011 Share Posted July 5, 2011 Can you explain what the following line is doing? $data[$id] = explode('"', $tmp[1], -1); It is removing the double quote from the right of the string so that you only left with the number you want after Goober. Quote Link to comment https://forums.phpfreaks.com/topic/241096-reading-through-file/#findComment-1238414 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.