|
Posted by Erwin Moller on 10/23/06 09:44
Tonino Greco wrote:
> Hi all,
>
> I wonder if someone on the list can offer a suggestion as to how I can
> parse a config file that has the form:
>
> group1
> name1
> name2
>
> group2
> name3
> name4
>
> group3
> name5
> name6
> name7
> name8
>
> ...
>
> I need to create a web interface to allow users to add/remove and
> generally modify the above config file.
>
> the name[*] is preceeded by 4 spaces and not a tab.
>
> Does anyone have any suggestions ?
>
> Many thanks
> Tonino
Hi Tony,
Read the file into some variable.
$file = file_get_contents ("/path/to/your/file");
// find the double newlines
$groupsArr = explode("\n\n",file);
// $groupsArr[0] now contains:
//group1
// name1
// name2
Depending on the structure you have in mind you can now continue with the
seperate groups like this (just an example)
In this example all groups are stored associatively in $myGroups
$myGroups = array();
foreach ($groupsArr as $oneGroup){
$lines = explode("\n",$oneGroup);
// first line contains groupname
$myGroup[$line[0]] = array();
// put the names in, but not the first
for($i=1;$i<count($lines);$i++){
$myGroup[$line[0]][] = trim($lines[$i]);
}
}
to view the result:
<pre>
<? print_r($myGroups); ?>
</pre>
Not tested.
Good luck,
Regards,
Erwin Moller
[Back to original message]
|