I have a config file which structure looks like this:
#comment1
key1 value1
#comment2
key-key2 value2
#comment3
key3 value3 value4
I have been trying to parse this in PHP with the following code:
$lines = explode(PHP_EOL, $string);
$config = [];
foreach ($lines as $l) {
preg_match("/^(?P<key>\w+)\s+(?P<value>.*)/", $l, $matches);
if (isset($matches['key'])) {
$config[$matches['key']] = $matches['value'];
}
}
However I am not able to get the regex right, the above regex is only functioning for the line with #comment1, it does not parse key-key2 properly and #comment3 key-value has 2 values which should just be 1 string inside the array.
Output I wish to have:
[
'key1' => 'value1',
'key-key2' => 'value2',
'key3' => 'value3 value4'
]
Anyone able to help me with the regex?
$linesis in fact just a single line ?