I have a program that, unfortunately, saves some information in a text file, making the text file almost like a database. So I decided to create a front end that is able to handle it.
An example of what's in the file:
Host {
Name = test1
Address = 192.168.0.1
Port = 8080
}
Host {
Name = test2
Address = 192.168.0.2
Port = 8080
}
To start I'm using fwrite() to insert new Hosts at the end of the file. But when I try to edit the Hosts through functions using seek() it doesn't work well because it counts byte by byte.
Ok, so I tried to create an array for this, to try to edit the data like the "Name". I'm actually basing a lot on this article: how to convert a string with brackets to an array in php.
Array ( [Host ] => Array ( [ Name = test1 Address = 192.168.0.1 Port = 8080 ] => Array ( ) ) )
Can anyone guide me in the right way to do it?
My code:
<?php
$input = shell_exec("cat /etc/Program/Hosts.conf");
$output = array();
$pointer = &$output;
while( ($index = strpos( $input, '{')) !== false) {
if( $index != 0) {
$key = substr( $input, 0, $index);
$pointer[$key] = array();
$pointer = &$pointer[$key];
$input = substr( $input, $index);
continue;
}
$end_index = strpos( $input, '}');
$array_key = substr( $input, $index + 1, $end_index - 1);
$pointer[$array_key] = array();
$pointer = &$pointer[$array_key];
$input = substr( $input, $end_index + 1);
}
print_r( $output);
?>