config file parse ?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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:





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

Re: config file parse ?

Tonino  Greco wrote:

Quoted text here. Click to load it

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:
//    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
   $myGroup[$line[0]][] = trim($lines[$i]);

to view the result:
<? print_r($myGroups); ?>

Not tested.

Good luck,

Erwin Moller

Re: config file parse ?


Quoted text here. Click to load it

thanks - I think that pushed me in the right direction.  I used array
of arrays to store the group names in the first array and then each
element of the array has an array of the names.

This way I can array_push new items and do as I please.

Thanks for the help

Site Timeline