PHP: reading config.ini for an array with file ()

My configuration file looks like this:

title = myTitle;
otherTitle = myOtherTitle;

when I read the file with file (), it creates this array

[0] => title = myTitle;
[1] => otherTitle = myOtherTitle;

and what I want the array to look like this:

[title] => myTitle;
[otherTitle] => myOtherTitle;

Am I using the wrong approach to her? Should I just read the entire configuration in the bite and blow it from there?

+3
source share
2 answers

You can use the function parse_ini_file. It is available in PHP 4 and 5.

If your configuration file looks like this:

one = 1;
five = 5;
animal = BIRD;

The function will return the following associative array:

Array
(
    [one] => 1
    [five] => 5
    [animal] => BIRD
)
+9
source

. , $config .

$config = array();
$lines = file("config.ini");
foreach ($lines as $line) {
    $vals = explode('=', $line, 2);
    $config[trim($vals[0])] = trim($vals[1]);
}
0

Source: https://habr.com/ru/post/1710881/


All Articles