PHP class that allows reading and writing infinitely (?) nested arrays in .ini files using dot notation.
Reads the string specified in str
, and returns the settings in it in an associative array.
The .ini string being parsed.
By setting the process_sections parameter to true, you get a multidimensional array, with the section names and settings included. The default for process_sections
is false.
Can either be INI_SCANNER_NORMAL (default) or INI_SCANNER_RAW. If INI_SCANNER_RAW is supplied, then option values will not be parsed. See the php docs for more info.
The settings are returned as an associative array on success. Empty array on failure.
Loads in the ini file specified in filename
, and returns the settings in it in an associative array.
The filename of the ini file being parsed. If a relative path is used, it is evaluated relative to the current working directory, then the include_path.
By setting the process_sections parameter to true, you get a multidimensional array, with the section names and settings included. The default for process_sections
is false.
Can either be INI_SCANNER_NORMAL (default) or INI_SCANNER_RAW. If INI_SCANNER_RAW is supplied, then option values will not be parsed. See the php docs for more info.
The settings are returned as an associative array on success. Empty array on failure.
Recursively iterates through a given array, creating a string based on the contents.
The array to be iterated through.
By setting the process_sections parameter to true, you get a multidimensional array, with the section names and settings included. The default for process_sections
is false.
The settings are returned as a string on success. Empty string on failure.
Iterates through a given array, writing its contents to the disk.
The array to be iterated through.
The path of the .ini file to be saved.
By setting the process_sections parameter to true, you get a multidimensional array, with the section names and settings included. The default for process_sections
is false.
If true, will append .php
to the end of the file name, as well as ;<?php exit(); ?>
to the first line of the file. The default for protected
is false.
The number of bytes that were written to the file, or false
on failure.
<?php
include_once $_SERVER["DOCUMENT_ROOT"] . "/path/to/INI.php";
$arr = array(
"foo" => array(
"bar" => array(
"baz" => "value"
)
)
);
$str = INI::from_array($arr));
var_dump($str);
// string(22) "foo.bar.baz = "value""
$parsed = INI::parse_string($str);
var_dump($parsed);
/*
array(1) {
["foo"]=>
array(1) {
["bar"]=>
array(1) {
["baz"]=>
string(5) "value"
}
}
}
*/
You can process .ini file sections by passing true
to the second argument to any of the methods.
$arr = array(
"foo" => array(
"bar" => array(
"baz" => "value"
)
)
);
$str = INI::from_array($arr), true);
var_dump($str);
/* string(22) "
[foo]
bar.baz = "value"
"
*/
When writing a .ini file using write_file()
, you can pass the protected
flag as a function parameter. This effectively protects the .ini file from being accessed externally, since it essentially becomes a php script. This does not affect the functionality of parse_file()
or parse_string()
.
$arr = array(
"foo" => array(
"bar" => array(
"baz" => "value"
)
)
);
INI::write_file($arr, "/path/to/file.ini", false, true);
/*
-- file.ini.php --
;<?php exit(); ?>
foo.bar.baz = "value"
*/
NOTE: When passing $protected = true
, the extension of the written file becomes .php, instead of .ini. As such, you should take care to reference it correctly when calling functions such as parse_file()
.
If any of your keys' names contain .
, you may escape it using single-quotes ('
).
/*
-- file.ini --
'foo.bar.baz' = "value"
*/
$arr = INI::parse_file("/path/to/file.ini", false);
var_dump($arr);
/*
array(1) {
["foo.bar.baz"]=>
string(5) "value"
}
*/