PHP code example of magicalex / write-ini-file

1. Go to this page and download the library: Download magicalex/write-ini-file library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

magicalex / write-ini-file example snippets




WriteiniFile\WriteiniFile;
use \WriteiniFile\ReadiniFile;

$data = [
    'fruit' => ['orange' => '100g', 'fraise' => '10g'],
    'legume' => ['haricot' => '20g', 'oignon' => '100g'],
    'jus' => ['orange' => '1L', 'pomme' => '1,5L', 'pamplemousse' => '0,5L'],
];

// demo create ini file
$file = new WriteiniFile('file.ini');
$file
    ->create($data)
    ->add(['music' => ['rap' => true, 'rock' => false]])
    ->rm(['jus' => ['pomme' => '1,5L']])
    ->update(['fruit' => ['orange' => '200g']])
    ->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=true
rock=false
*/

// Just read a file ini
var_dump(ReadiniFile::get('file.ini'));
/* output
array(4) {
  'fruit' => array(2) {
    'orange' => string(4) "200g"
    'fraise' => string(3) "10g"
  }
  'legume' => array(2) {
    'haricot' => string(3) "20g"
    'oignon' => string(4) "100g"
  }
  'jus' => array(2) {
    'orange' => string(2) "1L"
    'pamplemousse' => string(4) "0,5L"
  }
  'music' => array(2) {
    'rap' => string(4) "true"
    'rock' => string(5) "false"
  }
}
*/

$erase = (new WriteiniFile('file.ini'))->erase()->write();
// file.ini -> empty