【问题标题】:PHP function to edit configuration file编辑配置文件的PHP函数
【发布时间】:2016-01-07 21:23:16
【问题描述】:

我正在寻找可用于编辑文本文件中的键/值对的 PHP 函数。

我想做的事(PHP):

changeValue(key, bar);

并在settings.txt:

key = foo
key2 =foo2

改为:

key = bar
key2 = foo2

到目前为止我得到了什么(不工作):

function changeValue($input) {
    $file = file_get_contents('/path/to/settings.txt');
    preg_match('/\b$input[0]\b/', $file, $matches);
    $file = str_replace($matches[1], $input, $file);
    file_put_contents('/path/to/settings.txt', $file);
}

How to update an ini file with php? 让我开始了。我阅读了许多其他问题,但无法正常工作。

【问题讨论】:

  • 我想用它来存储和更改用户在网页上输入的小数据字符串。
  • 我假设这是遗留代码,这就是为什么你不使用 Yaml 或类似的东西。使用 PHP 在本地管理配置文件是一件令人头疼的事情。还有其他更好的方法,yaml 就是其中之一。
  • 用户是我,我将使用大约 10 个值。我想让它尽可能简单,但我会研究 Yaml。
  • 好的,但最简单的方法是使用 yaml 或类似的东西,而不是编写一堆自定义 PHP 代码。 webdeveloper.gdemolished.com/…

标签: php function key-value keyvaluepair


【解决方案1】:

我会使用 JSON,至少带有 JSON_PRETTY_PRINT 选项来写入和 json_decode() 来读取。

// read file into an array of key => foo
$settings = json_decode(file_get_contents('/path/to/settings.txt'), true);

// write array to file as JSON
file_put_contents('/path/to/settings.txt', json_encode($settings, JSON_PRETTY_PRINT));

这将创建一个文件,例如:

{
    "key": "foo",
    "key2": "bar",
    "key3": 1
}

另一种可能性是var_export() 使用类似的方法,或者您所要求的另一个简单示例:

// read file into an array of key => foo
$string = implode('&', file('/path/to/settings.txt', FILE_IGNORE_NEW_LINES));
parse_str($string, $settings);

// write array to file as key=foo
$data = implode("\n", $settings);
file_put_contents('/path/to/settings.txt', $data);

所以读入文件,更改设置$setting['key'] = 'bar';,然后写出来。

【讨论】:

  • 这不起作用,因为在文本文件中,\n 并不总是唯一的特殊中断字符,可能会导致很多处理错误。
  • 我会研究这个。为了清楚起见,文件中有多个键/值对(大约 10 个)。我还将编辑问题以反映这一点。
  • 我可以在限制条件下工作,例如没有空格或没有某些字符,只要我知道它们。
  • @DerekPollard:这不适用于任何文本文件,它适用于 OP 声明并控制的特定格式。但是请查看 JSON。
  • .txt 文件不完全接受 \n 作为换行符。 OP 可以控制一个 .txt。我的评论仍然成立。
【解决方案2】:

这不是使用 file_get_contents 使用文件,而是将每一行作为数组读取。 在你下面看到工作代码。写入数组有一点问题,增加了更多的中断,但不知道为什么。

changeValue("key", "test123"); 

function changeValue($key, $value) 
{
    //get each line as an array. 
    $file = file("test.txt"); 

    //go through the array, the value is references so when it is changed the value in the array is changed. 
    foreach($file as &$val) 
    { 
        //check if the string line contains the current key. If it contains the key replace the value. substr takes everything before "=" so not to run if the value is the same as the key. 
       if(strpos(substr($val, 0, strpos($val, "=")), $key) !== false)
        {
           //clear the string
           $val = substr($val, 0, strpos($val, "="));
           //add the value 
           $val .= "= " . $value; 
        }
    }
    //send the changed array writeArray(); 
    writeArray($file); 
 }

function writeArray($array) 
{
    $str = ""; 
    foreach($array as $value)
    {
        $str .= $value . "\n"; 
    }

    //write the array. 
    file_put_contents('test.txt', $str);
}

?>

【讨论】:

  • 这看起来很有希望。您能否在代码中添加一些注释,以便我了解它的实际工作原理?
  • 这样的?
  • 修复关键错误 :) 希望对您有所帮助
  • 投了赞成票,因为这也有效,可能对其他人来说是一个更好的解决方案。
  • 您的随机空白错误是由 writearray 中的 "\n" 引起的,请将其删除并将其放在 $value 之后您在 changeValue() 中添加值的位置
猜你喜欢
  • 2021-09-02
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
  • 2014-01-12
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多