PHP code example of pear / config_lite

1. Go to this page and download the library: Download pear/config_lite 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/ */

    

pear / config_lite example snippets



fig = new Config_Lite;



ig = new Config_Lite('test.ini');

echo $config->get('db', 'user'); // dionysis
echo $config->get(null, 'public_key_file'); // ~/.ssh/id_rsa.pub

if (true === $config->getBool(null, 'debug', true)) {
	echo $config;
}

// read with ArrayAccess
echo $config['db']['password']; // c2oiVnY!f8sf



ite with file locking
$config = new Config_Lite('test.ini', LOCK_EX);

$config->set('db', 'user', 'JohnDoe')
	->set('db', 'password', 'd0g1tcVs$HgIn1');

// set with ArrayAccess
$config['public_key_file'] = '~/.ssh/id_rsa.pub';
$config['general'] = array('lang' => 'de');

// save object to file
$config->save();


$config = new Config_Lite('test.ini');
$config->set('db', 'user', 'JohnDoe')
	->set('db', 'password', 'd0g1tcVs$HgIn1');

// set global bool 'debug' 
$config->set(null, 'debug', false);

// save object to file
try {
	$config->save();
} catch (Config_Lite_Exception $e) {
    echo "\n", 'Exception Message: ', $e->getMessage();
}



name = 'test.ini';

$config = new Config_Lite();

try {
	$config->write($filename, array(
			'public_key_file' =>  "~/.ssh/id_rsa.pub",
			'general' => array(
				'lang' => 'fr'
			),
			'db' => array(
				'user' => 'dionysis',
				'password' => 'd0g1tcVs$HgIn1'
			)
		)
	);
} catch (Config_Lite_Exception $exception) {
    printf("Failed to write file: %s.\n", $filename);
    printf("Exception Message: %s\n", $exception->getMessage());
    printf("Exception Stracktrace: %s\n", $exception->getTraceAsString());
}



ig = new Config_Lite();

$filename = sprintf(
    "php://filter/write=string.rot13/resource=%s", "test.ini"
);

$config->write($filename, array(
	    'public_key_file' =>  "~/.ssh/id_rsa.pub",
	    'general' => array(
	    'lang' => 'fr'
	),
	'db' => array(
		'user' => 'dionysis',
		'password' => 'd0g1tcVs$HgIn1'
		)
	)
);

// Writing to stdout
$config->write("php://stdout", array(
	    'public_key_file' =>  "~/.ssh/id_rsa.pub",
	    'general' => array(
	    'lang' => 'fr'
	),
	'db' => array(
		'user' => 'dionysis',
		'password' => 'd0g1tcVs$HgIn1'
		)
	)
);



$config->set(null, 'private_key_file', '~/.ssh/id_rsa');
// set with arrayaccess
$config['public_key_file'] = '~/.ssh/id_rsa.pub';

$config->sync();

echo $config->get(null, 'public_key_file');
// get with arrayaccess
echo $config['private_key_file'];


$config = new Config_Lite($filename);

foreach ($config as $section => $name) {
	if (is_array($name)) {
		$s .= sprintf("[%s]\n", $section);
		foreach ($name as $key => $val) {
			$s .= sprintf("\t%s = %s\n", $key, $val);
		}
	} else {
		$s .= sprintf("%s=%s\n", $section, $name);
	}
}

echo $s;




ig = new Config_Lite('regex-test.ini');

$regex = '/Hello \"(.*?)\"/';
$config->set(null, 'regex', base64_encode($regex));
// save object, here sync to read it back, just to test
$config->sync();
// in 'regex-test.ini': regex = "L0hlbGxvIFwiKC4qPylcIi8="
$regex = base64_decode($config->get(null, 'regex'));
if (preg_match($regex, 'Hello "World"!')) {
    printf("matched. regex:%s", $regex);
} else {
    printf("no match found. regex:%s", $regex);
}

{
    "repositories": [
        {
            "type": "pear",
            "url": "pear.php.net"
        }
    ],
    "


public_key_file =  "~/.ssh/id_rsa.pub"
debug = yes

[general]
lang = "en"

[db]
user = "dionysis"
password = "c2oiVnY!f8sf"