创建ini文件,在PHP中写入值

发布于 2024-07-30 19:25:46 字数 162 浏览 6 评论 0 原文

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

I cannot find a way that easily lets me create a new file, treat it as an ini file (not php.ini or simiilar... a separate ini file for per user), and create/delete values using PHP. PHP seems to offer no easy way to create an ini file and read/write/delete values. So far, it's all just "read" - nothing about creating entries or manipulating keys/values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情深已缘浅 2024-08-06 19:25:46

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

祝你好运!

Found following code snippet from the comments of the PHP documentation:

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; 
}

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

Good luck!

吝吻 2024-08-06 19:25:46

我使用这个,它似乎有效,

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 文件的文本

I use this and it seems to work

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";
        }
    }
 }

it returns the text to write to an .ini file

遗心遗梦遗幸福 2024-08-06 19:25:46

基于上面的答案,我写了这个可能有用的课程。 适用于 PHP 5.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";
                }
            }
        }
    }

based on the above answers I wrote this class that might be useful. For PHP 5.3 but can be easily adapted for previous versions.

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";
                }
            }
        }
    }
未央 2024-08-06 19:25:46

在这部分代码中:

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 中找到空键

in this portion of code:

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 must be replaced by $key or you would find empty keys in your .ini

野却迷人 2024-08-06 19:25:46

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

  • Config_Lite - 如果您只想要 .ini 文件
  • Config - 还读取 .php。 xml 文件

我宁愿使用经过良好测试的代码,也不愿编写自己的代码。

PEAR has two (unit tested) packages which do the task you are longing for:

  • Config_Lite - ideal if you only want .ini files
  • Config - reads also .php and .xml files

I'd rather use well tested code than writing my own.

情释 2024-08-06 19:25:46

我无法保证它的工作效果如何,但有一些建议可以实现与 parse_ini_file< 的文档页面上的 ="nofollow noreferrer">parse_ini_file()(即 write_ini_file,这不是标准 PHP 函数) /代码>。

您可以使用 write_ini_file 将值发送到文件,使用 parse_ini_file 将它们读回 - 修改 parse_ini_file 返回的关联数组,然后使用 write_ini_file 将修改后的数组写回文件。

那对你有用吗?

I can't vouch for how well it works, but there's some suggestions for implementing the opposite of parse_ini_file() (i.e. write_ini_file, which isn't a standard PHP function) on the documentation page for parse_ini_file.

You can use write_ini_file to send the values to a file, parse_ini_file to read them back in - modify the associative array that parse_ini_file returns, and then write the modified array back to the file with write_ini_file.

Does that work for you?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文