Reputation: 571
I have a PHP file which writes some information into a configuration file which looks a bit like this...
#Date=01/2012
text1=hello
text2=goodbye
etc...
This file is written as a cfg file to be used by a perl script, but I need the file to be read back into PHP so it can be edited each time for the perl script to use again.
How can this be done?
I previously had it write into a .csv file and be read back in, but then was told this was not what was needed and it had to be in a cfg file, so will it be just as easy to add the info back into the PHP script as it is when reading in a .csv file.
I think what is needed is to not have the text1 and text2 variables to be read in, but to have what is after the equals to be read back in so the hello and goodbye strings would be read back into the PHP.
Another thing is that when the user adds in more info to say text1 it will have more than one value e.g. they could change text1 to...
text1=hello,howareyou
Then that , would need to be a seperate entity within the PHP script so I'm guessing I would use the explode function to get the seperate pieces of data.
This data will be read into a table where the user can then edit the information ready for when the perl script gets run again.
My HTML would be something like this when the info is read in.
<table>
<tr>
<td>text 1</td>
<td>hello</td>
<td>howareyou</td>
</tr>
<tr>
<td>text 2</td>
<td>goodbye</td>
</tr>
</table>
I hope I have made myself clear with this and I am really struggling to comprehend how this could be done. As it doesn't seem as easy as reading from a .csv
Also I don't want the PHP to execute the script, I just need it to be read into the PHP so the user can edit the information again.
Upvotes: 0
Views: 1267
Reputation: 34556
Try this. You end up with a tidy associative array - param names as element names, and param values as element values.
$cfg_path = 'path/to/config.cfg';
if (file_exist($cfg_path)) {
if (preg_match_all('/^(?!#)(.+)=(.*)$/m', file_get_contents($cfg_path), $cfg)) {
$cfg = array_combine($cfg[1], $cfg[2]);
foreach($cfg as $name => $val)
echo $name.' = '.$val.'<br />';
}
}
Upvotes: 1
Reputation: 29932
Where is the format specified? Maybe you can use the .ini-file-format, which is similar to your example. You can use parse_ini_file()
to read in the .ini-file as array.
You also can group several entries. See the examples at the linked PHP documentation page.
Upvotes: 2