https://wiki.uniformserver.com/index.php?title=My_Ini_Design:_parse_ini_file&feed=atom&action=historyMy Ini Design: parse ini file - Revision history2024-03-29T06:12:15ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=My_Ini_Design:_parse_ini_file&diff=4791&oldid=prevOlajideolaolorun: Reverted edits by Upazixorys (Talk); changed back to last version by Ric2010-11-24T08:25:07Z<p>Reverted edits by <a href="/Special:Contributions/Upazixorys" title="Special:Contributions/Upazixorys">Upazixorys</a> (<a href="/index.php?title=User_talk:Upazixorys&action=edit&redlink=1" class="new" title="User talk:Upazixorys (page does not exist)">Talk</a>); changed back to last version by <a href="/User:Ric" title="User:Ric">Ric</a></p>
<a href="https://wiki.uniformserver.com/index.php?title=My_Ini_Design:_parse_ini_file&diff=4791&oldid=4622">Show changes</a>Olajideolaolorunhttps://wiki.uniformserver.com/index.php?title=My_Ini_Design:_parse_ini_file&diff=4622&oldid=prevUpazixorys at 01:13, 24 November 20102010-11-24T01:13:28Z<p></p>
<a href="https://wiki.uniformserver.com/index.php?title=My_Ini_Design:_parse_ini_file&diff=4622&oldid=3910">Show changes</a>Upazixoryshttps://wiki.uniformserver.com/index.php?title=My_Ini_Design:_parse_ini_file&diff=3910&oldid=prevRic: New page: {{Nav My Ini Design}} PHP’s parse_ini_file function allows you to read the content of an ini-file into an array proving easy access to all option values set. The array can be saved back ...2009-10-09T17:04:47Z<p>New page: {{Nav My Ini Design}} PHP’s parse_ini_file function allows you to read the content of an ini-file into an array proving easy access to all option values set. The array can be saved back ...</p>
<p><b>New page</b></p><div>{{Nav My Ini Design}}<br />
PHP’s parse_ini_file function allows you to read the content of an ini-file into an array proving easy access to all option values set. The array can be saved back to a file however all comments will have been stripped.<br />
<br />
To change values use my_ini_set covered on the previous pages. Combined this with the above function and you have the makings of an easily configurable cron timer.<br />
<br />
This page outlines requirements for such a cron timer.<br />
<br />
== Quick test ==<br />
{|<br />
|-valign="top"<br />
|<br />
Using the previous test set-up.<br />
<br />
* Edit '''test.php'''<br />
* Delete the '''examples of use''' section<br />
* Replace with the following two lines<br />
{|<br />
|-<br />
|<br />
<pre><br />
$ini_array = parse_ini_file("cron.ini", true);<br />
print_r($ini_array);<br />
</pre><br />
|}<br />
''Note'':<br />
* True: Loads each block as an array (preserves structure)<br />
* False: Loads everything into a flat array.<br /> Any option with a common name are overwritten<br />
<br />
<br />
* Run the batch file '''Run_test.bat'''<br />
* Result as shown on the right:<br />
|<br />
&nbsp;<br />
|<br />
<pre><br />
Array<br />
(<br />
[moodle] => Array<br />
(<br />
[path] => http://localhost/moodle/admin/cron.php<br />
[period] => daily<br />
[ref] =><br />
)<br />
[drupal] => Array<br />
(<br />
[path] => http://localhost/drupal/cron.php<br />
[period] => daily<br />
[ref] =><br />
)<br />
[dtdns] => Array<br />
(<br />
[path] => ..\..\plugins\dtdns_updater\dtdns_updater.php<br />
[period] => hourly<br />
[ref] =><br />
)<br />
)<br />
</pre><br />
|}<br />
'''''[[#top | Top]]'''''<br />
== Cron Loop ==<br />
For a Cron loop we need access to the main keys any values we wish to use can be accessed using standard array notation.<br />
<br />
Again the foreach function provides a neat solution as the following examples show.<br />
{|<br />
|-valign="top"<br />
|'''''Comment'''''<br />
|'''''Code'''''<br />
|'''''Results'''''<br />
|-valign="top"<br />
|<br />
* Code loads file into array<br />
* Foreach loop scans array<br />
* Main Keys printed<br />
|<br />
<pre><br />
$ini_array = parse_ini_file("cron.ini", true);<br />
foreach($ini_array as $key => $value){<br />
print $key."\n"; <br />
}<br />
</pre><br />
|<br />
<pre><br />
moodle<br />
drupal<br />
dtdns<br />
</pre><br />
|-valign="top"<br />
|<br />
* Code loads file into array<br />
* Foreach loop scans array<br />
* Main Keys printed<br />
* Array elements accessed<br />using standard notation<br />
* Values are printed.<br />
|<br />
<pre><br />
$ini_array = parse_ini_file("cron.ini", true);<br />
foreach($ini_array as $key => $value){<br />
print $key."\n";<br />
print $ini_array[$key]['path']." \n";<br />
print $ini_array[$key]['period']." \n";<br />
print $ini_array[$key]['ref']."test\n\n";<br />
}<br />
</pre><br />
|<br />
<pre><br />
moodle<br />
http://localhost/moodle/admin/cron.php<br />
daily<br />
test<br />
<br />
drupal<br />
http://localhost/drupal/cron.php<br />
daily124<br />
test<br />
<br />
dtdns<br />
..\..\plugins\dtdns_updater\dtdns_updater.php<br />
hourly<br />
test<br />
<br />
Press any key to continue . . .<br />
</pre><br />
|}<br />
<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Issues ==<br />
When converting static regex patterns to use variables it looks tricky.<br />
<br />
In reality its quite simple anything enclosed in single quotes is treated by PHP as being literal (use as seen). If enclosed in double quotes PHP replaces all variables with their current value.<br />
<br />
Assume $option = "fred123" <br />
*''''/$option/'''' The resulting regex pattern is '''/$option/'''<br />
*'''"/$option/"''' The resulting regex pattern is '''/fred123/'''<br />
'''''Note'':''' The two slashes '''//''' are delimiters effectively saying this is a regex pattern.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Final Functions ==<br />
The final functions are shown below and include some example code to show how they can be used.<br />
<pre><br />
<?php<br />
//==== Examples of use<br />
if(my_ini_set('cron.ini','drupal','period','daily')){<br />
print "Update OK\n";<br />
}<br />
else{<br />
print "Update Failed\n";<br />
}<br />
<br />
print my_ini_get('cron.ini','moodle','path')."\n";<br />
//==== End Examples of use<br />
<br />
//=== my_ini_set ==============================================================<br />
// Sets an option value in a configuration file<br />
// Inputs:<br />
// $file Path to file including file name<br />
// $block Name of block excluding []<br />
// $option Name of option<br />
// $optionvalue New value of option<br />
// Outputs:<br />
// Returns true on success or false on failure <br />
<br />
function my_ini_set($file,$block,$option,$optionvalue){<br />
$ini_array = file("$file"); // Read file into array<br />
<br />
$flag=false; // Set found flag<br />
foreach($ini_array as $key => $value){ // Scan array line by line<br />
<br />
if($flag && preg_match('/^\s*\[/',$value)){ // Is it a new block <br />
$flag=false; // Yes: Rest flag<br />
}<br />
<br />
if(trim($value) == "[$block]"){ // Is it required block <br />
$flag=true; // Yes: set found flag <br />
}<br />
<br />
if($flag && preg_match("/^\s*$option/",$value)){ // Is required option <br />
$ini_array[$key] = "$option = $optionvalue\n"; // Yes save new value<br />
file_put_contents("$file",$ini_array); // Save array to file<br />
return true; // Success return true <br />
} <br />
}<br />
return false; // Option not found<br />
}//end<br />
//=== END my_ini_set ==========================================================<br />
<br />
<br />
//=== my_ini_get ==============================================================<br />
// Obtains an option value from a configuration file<br />
// Inputs:<br />
// $file Path to file including file name<br />
// $block Name of block excluding []<br />
// $option Name of option<br />
// Outputs:<br />
// Returns Value on success<br />
// Returns Blank on failure <br />
<br />
function my_ini_get($file,$block,$option){<br />
$ini_array = file("$file"); // Read file into array<br />
<br />
$flag=false; // Set found flag<br />
foreach($ini_array as $key => $value){ // Scan array line by line<br />
<br />
if($flag && preg_match('/^\s*\[/',$value)){ // Is it a new block <br />
$flag=false; // Yes: Rest flag <br />
}<br />
<br />
if(trim($value) == "[$block]"){ // Is it required block <br />
$flag=true; // Yes: set found flag <br />
}<br />
<br />
if($flag && preg_match("/^\s*$option/",$value)){ // Is required option <br />
$str = trim($ini_array[$key]); // Yes read and clean <br />
$str1 = preg_replace("/^$option\s*=\s*/", "", $str); // Cean <br />
return $str1; // Retun option value <br />
} <br />
}<br />
return ""; // Option not found <br />
}<br />
//=== END my_ini_get ==========================================================<br />
?><br />
</pre><br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Summary ==<br />
Above completes our test code for the second function.<br />
<br />
The two snippets of code are proof of concept on the next page these are converted into functions.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
[[Category: Uniform Server 5.0-Nano]]</div>Ric