HarryMW Posted February 20, 2011 Share Posted February 20, 2011 Hi there, I need to know how to code a php file, to jump to a specific point in a text file. I haven't a clue what you would use and need the file to do very simply, or so it seems to find the line starting with: "!CLIENTS", then go to the next line and take the information from thereon out UNTIL it reaches a blank line, where it would dis-regard it and stop. Is that possible? Link to comment https://forums.phpfreaks.com/topic/228241-jumping-to-a-point-in-a-text-file/ Share on other sites More sharing options...
litebearer Posted February 20, 2011 Share Posted February 20, 2011 what is the structure of the text file Link to comment https://forums.phpfreaks.com/topic/228241-jumping-to-a-point-in-a-text-file/#findComment-1177007 Share on other sites More sharing options...
HarryMW Posted February 20, 2011 Author Share Posted February 20, 2011 what is the structure of the text file ; Here there will be a few lines of notes from previous developers ; Notice the ";" character marks a comment line ; Blah ; Blah !VOICE voicename:hostname:otherinfo:otherinfo:otherinfo: voicename:hostname:otherinfo:otherinfo:otherinfo: voicename:hostname:otherinfo:otherinfo:otherinfo: voicename:hostname:otherinfo:otherinfo:otherinfo: voicename:hostname:otherinfo:otherinfo:otherinfo: !CLIENTS clientinfo:clientinfo:clientinfo:clientinfo:clientinfo: clientinfo:clientinfo:clientinfo:clientinfo:clientinfo: clientinfo:clientinfo:clientinfo:clientinfo:clientinfo: clientinfo:clientinfo:clientinfo:clientinfo:clientinfo: Theres an example, the datachunks are separated by the ":" character. Link to comment https://forums.phpfreaks.com/topic/228241-jumping-to-a-point-in-a-text-file/#findComment-1177010 Share on other sites More sharing options...
litebearer Posted February 20, 2011 Share Posted February 20, 2011 Although I am certain there are more 'elegant' ways, here is a VERY rough 'sledge-hammer' approach. It presumes your text file will always follow that format. <?PHP $file = "data.txt"; $haystack = file_get_contents($file); $needle = "!VOICE"; $my_array = explode($needle,$haystack); $haystack = $my_array[1]; $needle = "!CLIENTS"; $my_array = explode($needle,$haystack); $haystack =$my_array[0]; $haystack = str_replace("\n", "|", $haystack); $array_01 = explode("|", $haystack); array_shift($array_01); array_pop($array_01); array_pop($array_01); $nc = count($array_01); for($i=0;$i<$nc;$i++) { $temp_array = explode(":",$array_01[$i]); print_r($temp_array); echo "<hr>"; } ?> example http://www.nstoia.com/sat/test/test0001/ Link to comment https://forums.phpfreaks.com/topic/228241-jumping-to-a-point-in-a-text-file/#findComment-1177018 Share on other sites More sharing options...
HarryMW Posted February 20, 2011 Author Share Posted February 20, 2011 Hi there, Thank you for what you have pulled up and yes it works for that purpose, however all I need the code to do is go to the specific line starting "!CLIENTS", jump down a line- and pull the information from there on out until it reaches an empty line, where it will stop. This is so the query that will update and insert records if needed will only work for that block of information. Doesn't need to be outputted at that point. Harry. Link to comment https://forums.phpfreaks.com/topic/228241-jumping-to-a-point-in-a-text-file/#findComment-1177020 Share on other sites More sharing options...
PaulRyan Posted February 20, 2011 Share Posted February 20, 2011 Do you have a specific format you're looking for? Give us an example of what you want to end up with. I'm pretty sure you could use Litebearer's suggested code and work with it, especially if doing queries etc. Simply formatted data that you can use as you want. Regards, PaulRyan. Link to comment https://forums.phpfreaks.com/topic/228241-jumping-to-a-point-in-a-text-file/#findComment-1177033 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.