代码之家  ›  专栏  ›  技术社区  ›  netrox

创建ini文件,用PHP编写值

  •  44
  • netrox  · 技术社区  · 15 年前

    我找不到一种方法可以轻松地创建一个新文件,将其视为一个ini文件(不是php.ini或similar…每个用户一个单独的ini文件),并使用php创建/删除值。PHP似乎没有提供创建ini文件和读/写/删除值的简单方法。到目前为止,这一切都只是“阅读”——与创建条目或操纵键/值无关。

    6 回复  |  直到 15 年前
        1
  •  54
  •   Harikrishnan    10 年前

    从PHP文档的注释中找到以下代码片段:

    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 $key2=>$elem2) { 
                    if(is_array($elem2)) 
                    { 
                        for($i=0;$i<count($elem2);$i++) 
                        { 
                            $content .= $key2."[] = \"".$elem2[$i]."\"\n"; 
                        } 
                    } 
                    else if($elem2=="") $content .= $key2." = \n"; 
                    else $content .= $key2." = \"".$elem2."\"\n"; 
                } 
            } 
        } 
        else { 
            foreach ($assoc_arr as $key=>$elem) { 
                if(is_array($elem)) 
                { 
                    for($i=0;$i<count($elem);$i++) 
                    { 
                        $content .= $key."[] = \"".$elem[$i]."\"\n"; 
                    } 
                } 
                else if($elem=="") $content .= $key." = \n"; 
                else $content .= $key." = \"".$elem."\"\n"; 
            } 
        } 
    
        if (!$handle = fopen($path, 'w')) { 
            return false; 
        }
    
        $success = fwrite($handle, $content);
        fclose($handle); 
    
        return $success; 
    }
    

    用法:

    $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);
    

    祝你好运

        2
  •  7
  •   cweiske agentofuser    14 年前

    PEAR有两个(经过单元测试的)包,可以完成您渴望的任务:

        3
  •  7
  •   NullUserException Mark Roddy    13 年前

    我不能保证它工作得有多好,但有一些建议可以实现与之相反的功能 parse_ini_file() (即。 write_ini_file ,这不是一个标准的PHP函数)的文档页上 parse_ini_file .

    你可以用 要将值发送到文件, 解析ini文件 返回,然后使用 .

    这对你有用吗?

        4
  •  5
  •   mario-mesiti    14 年前

    else { 
        foreach ($assoc_arr as $key=>$elem) { 
            if(is_array($elem)) 
            { 
                for($i=0;$i<count($elem);$i++) 
                { 
                    $content .= $key2."[] = \"".$elem[$i]."\"\n"; 
                } 
            } 
            else if($elem=="") $content .= $key2." = \n"; 
            else $content .= $key2." = \"".$elem."\"\n"; 
        } 
    } 
    

    $key2必须替换为$key,否则您会在.ini中找到空键

        5
  •  3
  •   Tivie    11 年前

    基于以上的答案,我写了这个可能有用的课程。适用于PHP5.3,但可以轻松适应以前的版本。

    class Utils
        {
            public static function write_ini_file($assoc_arr, $path, $has_sections)
            {
                $content = '';
    
                if (!$handle = fopen($path, 'w'))
                    return FALSE;
    
                self::_write_ini_file_r($content, $assoc_arr, $has_sections);
    
                if (!fwrite($handle, $content))
                    return FALSE;
    
                fclose($handle);
                return TRUE;
            }
    
            private static function _write_ini_file_r(&$content, $assoc_arr, $has_sections)
            {
                foreach ($assoc_arr as $key => $val) {
                    if (is_array($val)) {
                        if($has_sections) {
                            $content .= "[$key]\n";
                            self::_write_ini_file_r(&$content, $val, false);
                        } else {
                            foreach($val as $iKey => $iVal) {
                                if (is_int($iKey))
                                    $content .= $key ."[] = $iVal\n";
                                else
                                    $content .= $key ."[$iKey] = $iVal\n";
                            }
                        }
                    } else {
                        $content .= "$key = $val\n";
                    }
                }
            }
        }
    
        6
  •  2
  •   user1123382    13 年前

    我用这个,它似乎很管用

    function listINIRecursive($array_name, $indent = 0)
    {
        global $str;
        foreach ($array_name as $k => $v)
        {
            if (is_array($v))
            {
                for ($i=0; $i < $indent * 5; $i++){ $str.= " "; }
                $str.= " [$k] \r\n";
                listINIRecursive($v, $indent + 1);
            }
                else
            {
                for ($i=0; $i < $indent * 5; $i++){ $str.= " "; }
                $str.= "$k = $v \r\n";
            }
        }
     }
    

    它返回要写入.ini文件的文本