Oki, two missing pieces left. First the code:
<pre>
<?php
function parse_tree($file_path) {
$oFile = fopen($file_path, 'r');
$aReturnArray = array();
while (!feof($oFile)) {
rtrim($oFile,"\r");
$sLine = fgets($oFile);
// rtrim($oFile,"\r");
$aRowItem = explode("=", $sLine);
$aTypeItem = explode(".", $aRowItem[0]);
// if substr_count($aTypeItem, ".",1) {
// $aTypeItem = explode(".", @aRowItem[0]);
// }
$sType = $aTypeItem[0];
$sAttr = $aTypeItem[1];
$sVal = $aRowItem[1];
if($sAttr == "")
{
$aReturnArray[$sType] = $sVal;
}
else
{
$aReturnArray[$sType][$sAttr] = $sVal;
}
}
return $aReturnArray;
}
var_dump(parse_tree("settings.txt"));
?>
</pre>
This outputs almost as I want it:
array(3) {
["server"]=>
array(4) {
["ipaddress"]=>
string(15) "192.168.50.72
"
["port"]=>
string(4) "80
"
["name"]=>
string(9) "staging
"
["database"]=>
string(13) "staginguser
"
}
["debugmode"]=>
string(3) "0
"
["client"]=>
array(2) {
["timeout"]=>
string(6) "3600
"
["autoconnect"]=>
string(1) "1"
}
}
For some reason there is a linefeed in the strings. I have tried to use rtrim($oFile,"\r"); but without any luck. I dubbelchecked the file and the only thing in the end of every line is a Carriage Return + Line Feed.
Also, the content doesn't get grouped the right way if there is two dots. Like server.database.type=mysql
server.database.username=staginguser
I tried substr_count(), but couldn't get it to work. I commentet out where I put rtrim() and substr_count()