PHP Operating INI configuration file
Copy CodeThe code is as follows:
Write INI file
function Write_ini_file ($assoc _arr, $path, $has _sections=false)
{
$content = "";
if ($has _sections)
{
foreach ($assoc _arr as $key = $elem)
{
$content. = "[". $key. "] \ n ";
foreach ($elem as $key 2=> $elem 2)
{
if (Is_array ($elem 2))
{
for ($i =0; $i {
$content. = $key 2. " [] = \ "". $elem 2[$i]. " \ "\ n";
}
}
else if ($elem 2== "") $content. = $key 2. "= \ n";
else $content. = $key 2. "= \" ". $elem 2." \ "\ n";
}
}
}
Else
{
foreach ($assoc _arr as $key = $elem)
{
if (Is_array ($elem))
{
for ($i =0; $i {
$content. = $key 2. " [] = \ "". $elem [$i]. " \ "\ n";
}
}
else if ($elem = = "") $content. = $key 2. "= \ n";
else $content. = $key 2. "= \" ". $elem." \ "\ n";
}
}
if (! $handle = fopen ($path, ' W '))
{
return false;
}
if (!fwrite ($handle, $content))
{
return false;
}
Fclose ($handle);
return true;
}
Usage
//
$sampleData = Array (
' First ' = = Array (
' First-1 ' = 1,
' First-2 ' = 2,
' First-3 ' = 3,
' First-4 ' = 4,
' First-5 ' = 5,
),
' Second ' = Array (
' Second-1 ' = 1,
' Second-2 ' = 2,
' Second-3 ' = 3,
' Second-4 ' = 4,
' Second-5 ' = 5,
));
Write_ini_file ($sampleData, './data.ini ', true);
Read INI file
Public Function Readini ($name)
{
if (file_exists (sem_path. ' init/'. $name))
{
$data = Parse_ini_file (sem_path. ' init/'. $name, True);
if ($data)
{
return $data;
}
}
Else
{
return false;
}
}
If you are working in Python, you need to remove the double quotes around the value of the configuration file generated by PHP. That is, the above generated INI configuration file function \ "Remove the OK."
http://www.bkjia.com/PHPjc/326785.html www.bkjia.com true http://www.bkjia.com/PHPjc/326785.html techarticle php Operation INI configuration file copy code code as follows:? PHP//write INI file function write_ini_file ($assoc _arr, $path, $has _sections=false) {$content = ""; if ($has _sections) {FOREAC ...