How to manipulate INI configuration files in php _php tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.