search

Home  >  Q&A  >  body text

php writes ini file

Use native php to read ini configurationparse_ini_file()Function
What function should be used to modify configuration items?

代言代言2768 days ago915

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-06-28 09:24:40

    composer require piwik/ini

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-28 09:24:40

    There is no native support for modifying the configuration. You can implement it yourself or use someone else’s wheel

    I am a porter

    https://packagist.org/package...

    <?php
    
    require 'vendor/autoload.php';
    
    use WriteiniFile\WriteiniFile;
    
    $data = [
        'fruit' => ['orange' => '100g', 'fraise' => '10g'],
        'legume' => ['haricot' => '20g', 'oignon' => '100g'],
        'jus' => ['orange' => '1L', 'pomme' => '1,5L', 'pamplemousse' => '0,5L'],
    ];
    
    // demo create ini file
    $a = new WriteiniFile('file.ini');
    $a->create($data);
    $a->add([
        'music' => ['rap' => true, 'rock' => false]
    ]);
    $a->rm([
        'jus' => ['pomme' => '1,5L']
    ]);
    $a->update([
        'fruit' => ['orange' => '200g'] // 100g to 200g
    ]);
    $a->write();
    
    echo '<pre>'.file_get_contents('file.ini').'</pre>';
    
    /* output file.ini
    [fruit]
    orange = "200g"
    fraise = "10g"
    
    [legume]
    haricot = "20g"
    oignon = "100g"
    
    [jus]
    orange = "1L"
    pamplemousse = "0,5L"
    
    [music]
    rap = 1
    rock = 0
    */
    
    $b = new WriteiniFile('file.ini');
    $b->erase();
    $b->write();
    

    reply
    0
  • Cancelreply