PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
compound_eye
Forum Newbie
Posts: 15 Joined: Wed Mar 17, 2004 8:42 pm
Post
by compound_eye » Wed Mar 17, 2004 8:42 pm
if i have an ini file called "xhtml.ini"
that looks something like this
Code: Select all
їxhtml]
className = FormatEngineXHTML
classFile = "FormatEngineXHTML.class.php"
їwrapperManager]
className = WrapperManager
classFile = "WrapperManager.class.php"
їdefaultWrapper]
defaultWrapper = xhtml
then i can use:
Code: Select all
$setting = parse_ini_file("xhtml.ini", true);
to get a 2 dimensional array of those ini file settings
so
Code: Select all
echo $settingї'wrapperManager']ї'className'];
would give:
WrapperManager
is there a way to make an ini file with more than 2 dimensions?
thanks,
Mathew
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Mar 17, 2004 8:52 pm
You can do this with
PEAR's Config but not with parse_ini_file as far as i know.
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Wed Mar 17, 2004 9:06 pm
I might be totaly out in the blue but:
Code: Select all
$setting = parse_ini_file("xhtml.ini",true);
foreach ($setting['wrapperManager'] as $k => $v) {
if (ereg ("\[(.*)\]", $k, $reg)) {
$setting['wrapperManager'][$reg[1]][] = $v;
unset($setting['wrapperManager'][$k]);
}
}
print_r($setting);
Just testing on one area, needs tweaking...
Source:
Code: Select all
їwrapperManager]
classNameїfoo] = WrapperManagerFoo
classFileїfoo] = "WrapperManagerFoo.class.php"
classNameїbar] = WrapperManagerBar
classFileїbar] = "WrapperManagerBar.class.php"
Result:
Code: Select all
Array
(
їxhtml] => Array
(
їclassName] => FormatEngineXHTML
їclassFile] => FormatEngineXHTML.class.php
)
їwrapperManager] => Array
(
їfoo] => Array
(
ї0] => WrapperManagerFoo
ї1] => WrapperManagerFoo.class.php
)
їbar] => Array
(
ї0] => WrapperManagerBar
ї1] => WrapperManagerBar.class.php
)
)
)
compound_eye
Forum Newbie
Posts: 15 Joined: Wed Mar 17, 2004 8:42 pm
Post
by compound_eye » Wed Mar 17, 2004 10:11 pm
you know i think ereg might just be the answer...
if i have the ini file:
Code: Select all
їxhtml]
className = FormatEngineXHTML
classFile = "FormatEngineXHTML.class.php"
їwrapperManager]
WrapperManagerїclassName] = WrapperManager
WrapperManagerїclassFile] = "WrapperManager.class.php"
GiantMonkeyRobotїclassName] = GiantMonkeyRobot
GiantMonkeyRobotїclassFile] = "GiantMonkeyRobot.class.php"
їdefaultWrapper]
defaultWrapper = xhtml
and the code:
Code: Select all
<?php
<?php
$setting = parse_ini_file('test.ini',true);
foreach ($setting as $outer_key => $outer_value)
{
foreach ($setting[$outer_key] as $key => $value)
{
if (ereg ("\[(.*)\]", $key, $reg))
{
$inner_key = substr ( $key, 0 , strpos($key,'['));
$setting[$outer_key][$inner_key][$reg[1]] = $value;
unset($setting[$outer_key][$key]);
}
}
}
print_r($setting);
?>
?>
i get:
Code: Select all
Array
(
їxhtml] => Array
(
їclassName] => FormatEngineXHTML
їclassFile] => FormatEngineXHTML.class.php
)
їwrapperManager] => Array
(
їWrapperManager] => Array
(
їclassName] => WrapperManager
їclassFile] => WrapperManager.class.php
)
їGiantMonkeyRobot] => Array
(
їclassName] => GiantMonkeyRobot
їclassFile] => GiantMonkeyRobot.class.php
)
)
їdefaultWrapper] => Array
(
їdefaultWrapper] => xhtml
)
)
this seems to work,
i wonder if there is a way to make this a recursive function so that you could have an arbitrary depth to the array?
thanks very much for you help
mathew
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Thu Mar 18, 2004 5:42 am
Not sure what you mean (english isn't really my flavour), but post an example of your ini file 'idea' of how you like it to be. Might help (me).