0

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);
?>
5
  • As you haven't shared a single line of code, it's impossible to tell you where this is going wrong. Also, if you think that writing to this file is a bad idea, why not refactor your application to write this data to a proper database? Commented Feb 8, 2022 at 14:58
  • So you basically want to convert it to an array? Commented Feb 8, 2022 at 15:17
  • @ruleboy21, I intend to convert it to an array first and then manipulate the data, such as the Host, Address and Port. I think it's a good way to do this, but you can give your suggestion. Commented Feb 8, 2022 at 15:25
  • Hello @NicoHaase! I edited the post. I'm basically using the same code as the post I used as a reference. About refactoring my app, I didn't actually develop it. So I'm trying to improve it for common users. Commented Feb 8, 2022 at 15:41
  • You could make your life easier and append json_encoded (single) lines that you later read one line at a time and decode. Would save you from parsing a custom format. Commented Feb 8, 2022 at 18:19

1 Answer 1

0

You can use file() to convert the file to array of lines and then manipulate the array to get array of hosts. Try this

function hostsToArray($filepath){
    $file  = array_map('trim', file($filepath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
    $array = [];
    $i     = 0;

    foreach ($file as $line) {
        if( $line === 'Host {' ){
            $array[$i] = [];
        }else if( $line === '}' ){
            $i++;
        }else{
            list($key, $value) = explode(' = ', $line);
            $array[$i][$key] = $value;
        }
    }

    return $array;
}

$hosts = hostsToArray('/etc/Program/Hosts.conf');
print_r($hosts);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! It worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.