I'm using ereg to parse a config file, but I'm getting the first letter of each value from the config file chopped off when I enter the data into a multidimensional array. The format of the config file is as follows
[tclog]
errmask = 1 # 1=error to screen only, 2=to file only, 3= both
logfile = ./log/system.log # the logfile to use
[halflifemonitor]
listenport = 27069 #what port to listen on
ipaddress = 192.168.0.5 #the ip of this server (needed for now)
[servermonitor]
maxbufsize = 10 #how large to buffer incoming network things before writing them
I have a general file I/O class that reads a file into an array using file(filename), but for reading a config file in a specific format I'm changing from a single dimension array into a multidimensional array of the following
$array[classname][variablename] = value;
$array[classname][variablename]["comment"] = "#comment";
where "classname" is the name of the class that uses this cfg value, "variablename" is the variable in the cfg file and comment is the comment possible associated with this variable.
The code for converting a single dimension array to my data structure is below.
$newfile;
$size = count($this->filearray);
//Variable to hold the indexname for the next part of the array
$classindex = "NULL";
for( $i=0; $i<$size; $i++ ) {
$classreg = "\[([a-zA-Z]+)\]";
$varreg = "([a-zA-Z0-9]+)( )*=( )*([a-zA-Z0-9./]+)( )*(#(.*))?";
$commentreg = "#(.*)";
$line = $this->filearray[$i];
//if it is a classname, put that name as the index
if( ereg($classreg, $line, $regs ) ) {
$classindex = $regs[1];
}
//now each entry is a member of that class until the next match changes it
else if( ereg($varreg, $line, $regs ) ) {
$newfile[$classindex][$regs[1]] = $regs[4];
$newfile[$classindex][$regs[1]]["comment"] = $regs[7];
}
}
The regular expressions work (I'm sure there's better ways of doing it, but I'm not too crash hot with them 😉), and if I echo out what the $regs[x] are they come out correct. However, after the array is built, if I echo out $newfile["halflifemonitor"]["listenport"] I get w7089. It's the same with all the others... the first character is borked, as follows
Array ( [tclog] => Array ( [errmask] => [logfile] => /log/system.log )
[halflifemonitor] => Array ( [listenport] => w7069 [ipaddress] => t92.168.0.5 )
[servermonitor] => Array ( [maxbufsize] => h0 ) )
I have no idea what's causing this weirdness. Any ideas?