Jump to content

Jumping to a point in a text file


HarryMW

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.