My Ini Design: parse ini file: Difference between revisions

Jump to navigation Jump to search
m
Reverted edits by Upazixorys (Talk); changed back to last version by Ric
No edit summary
m (Reverted edits by Upazixorys (Talk); changed back to last version by Ric)
 
Line 1: Line 1:
=[http://uwujojedeh.co.cc Page Is Unavailable Due To Site Maintenance, Please Visit Reserve Copy Page]=
{{Nav My Ini Design}}
{{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 to a file however all comments will have been stripped.
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.
Line 9: Line 8:
== Quick test ==
== Quick test ==
{|
{|
|-valign="top"
|-valign="top"
|
|
Using the previous test set-up.
Using the previous test set-up.
Line 19: Line 18:
|-
|-
|
|
<pre>
<pre>
$ini_array = parse_ini_file(&quot;cron.ini&quot;, true);
$ini_array = parse_ini_file("cron.ini", true);
print_r($ini_array);
print_r($ini_array);
&lt;/pre&gt;
</pre>
|}
|}
''Note'':
''Note'':
* True: Loads each block as an array (preserves structure)
* True: Loads each block as an array (preserves structure)
* False: Loads everything into a flat array.&lt;br /&gt; Any option with a common name are overwritten
* False: Loads everything into a flat array.<br /> Any option with a common name are overwritten




Line 32: Line 31:
* Result as shown on the right:
* Result as shown on the right:
|
|
&amp;nbsp;
&nbsp;
|
|
&lt;pre&gt;
<pre>
Array
Array
(
(
     [moodle] =&gt; Array
     [moodle] => Array
         (
         (
             [path] =&gt; http://localhost/moodle/admin/cron.php
             [path] => http://localhost/moodle/admin/cron.php
             [period] =&gt; daily
             [period] => daily
             [ref] =&gt;
             [ref] =>
         )
         )
     [drupal] =&gt; Array
     [drupal] => Array
         (
         (
             [path] =&gt; http://localhost/drupal/cron.php
             [path] => http://localhost/drupal/cron.php
             [period] =&gt; daily
             [period] => daily
             [ref] =&gt;
             [ref] =>
         )
         )
     [dtdns] =&gt; Array
     [dtdns] => Array
         (
         (
             [path] =&gt; ..\..\plugins\dtdns_updater\dtdns_updater.php
             [path] => ..\..\plugins\dtdns_updater\dtdns_updater.php
             [period] =&gt; hourly
             [period] => hourly
             [ref] =&gt;
             [ref] =>
         )
         )
)
)
&lt;/pre&gt;
</pre>
|}
|}
'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
Line 64: Line 63:
Again the foreach function provides a neat solution as the following examples show.
Again the foreach function provides a neat solution as the following examples show.
{|
{|
|-valign=&quot;top&quot;
|-valign="top"
|'''''Comment'''''
|'''''Comment'''''
|'''''Code'''''
|'''''Code'''''
|'''''Results'''''
|'''''Results'''''
|-valign=&quot;top&quot;
|-valign="top"
|
|
* Code loads file into array
* Code loads file into array
Line 74: Line 73:
* Main Keys printed
* Main Keys printed
|
|
&lt;pre&gt;
<pre>
$ini_array = parse_ini_file(&quot;cron.ini&quot;, true);
$ini_array = parse_ini_file("cron.ini", true);
foreach($ini_array as $key =&gt; $value){
foreach($ini_array as $key => $value){
   print $key.&quot;\n&quot;;  
   print $key."\n";  
}
}
&lt;/pre&gt;
</pre>
|
|
&lt;pre&gt;
<pre>
moodle
moodle
drupal
drupal
dtdns
dtdns
&lt;/pre&gt;
</pre>
|-valign=&quot;top&quot;
|-valign="top"
|
|
* Code loads file into array
* Code loads file into array
* Foreach loop scans array
* Foreach loop scans array
* Main Keys printed
* Main Keys printed
* Array elements accessed&lt;br /&gt;using standard notation
* Array elements accessed<br />using standard notation
* Values are printed.
* Values are printed.
|
|
&lt;pre&gt;
<pre>
$ini_array = parse_ini_file(&quot;cron.ini&quot;, true);
$ini_array = parse_ini_file("cron.ini", true);
foreach($ini_array as $key =&gt; $value){
foreach($ini_array as $key => $value){
   print $key.&quot;\n&quot;;
   print $key."\n";
   print $ini_array[$key]['path'].&quot; \n&quot;;
   print $ini_array[$key]['path']." \n";
   print $ini_array[$key]['period'].&quot; \n&quot;;
   print $ini_array[$key]['period']." \n";
   print $ini_array[$key]['ref'].&quot;test\n\n&quot;;
   print $ini_array[$key]['ref']."test\n\n";
}
}
&lt;/pre&gt;
</pre>
|
|
&lt;pre&gt;
<pre>
moodle
moodle
http://localhost/moodle/admin/cron.php
http://localhost/moodle/admin/cron.php
Line 121: Line 120:


Press any key to continue . . .
Press any key to continue . . .
&lt;/pre&gt;
</pre>
|}
|}


Line 132: Line 131:
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.
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.


Assume $option = &quot;fred123&quot;
Assume $option = "fred123"
*''''/$option/'''' The resulting regex pattern is '''/$option/'''
*''''/$option/'''' The resulting regex pattern is '''/$option/'''
*'''&quot;/$option/&quot;''' The resulting regex pattern is '''/fred123/'''
*'''"/$option/"''' The resulting regex pattern is '''/fred123/'''
'''''Note'':''' The two slashes '''//''' are delimiters effectively saying this is a regex pattern.
'''''Note'':''' The two slashes '''//''' are delimiters effectively saying this is a regex pattern.


Line 141: Line 140:
== Final Functions ==
== Final Functions ==
The final functions are shown below and include some example code to show how they can be used.
The final functions are shown below and include some example code to show how they can be used.
&lt;pre&gt;
<pre>
&lt;?php
<?php
//==== Examples of use
//==== Examples of use
if(my_ini_set('cron.ini','drupal','period','daily')){
if(my_ini_set('cron.ini','drupal','period','daily')){
  print &quot;Update OK\n&quot;;
  print "Update OK\n";
}
}
else{
else{
  print &quot;Update Failed\n&quot;;
  print "Update Failed\n";
}
}


print my_ini_get('cron.ini','moodle','path').&quot;\n&quot;;
print my_ini_get('cron.ini','moodle','path')."\n";
//==== End Examples of use
//==== End Examples of use


Line 165: Line 164:


function my_ini_set($file,$block,$option,$optionvalue){
function my_ini_set($file,$block,$option,$optionvalue){
  $ini_array = file(&quot;$file&quot;);                        // Read file into array
  $ini_array = file("$file");                        // Read file into array


  $flag=false;                                        // Set found flag
  $flag=false;                                        // Set found flag
  foreach($ini_array as $key =&gt; $value){              // Scan array line by line
  foreach($ini_array as $key => $value){              // Scan array line by line


   if($flag &amp;&amp; preg_match('/^\s*\[/',$value)){      // Is it a new block  
   if($flag &&  preg_match('/^\s*\[/',$value)){      // Is it a new block  
     $flag=false;                                    // Yes: Rest flag
     $flag=false;                                    // Yes: Rest flag
   }
   }


   if(trim($value) == &quot;[$block]&quot;){                  // Is it required block               
   if(trim($value) == "[$block]"){                  // Is it required block               
     $flag=true;                                    // Yes: set found flag                           
     $flag=true;                                    // Yes: set found flag                           
   }
   }
   
   
   if($flag &amp;&amp; preg_match(&quot;/^\s*$option/&quot;,$value)){    // Is required option   
   if($flag &&  preg_match("/^\s*$option/",$value)){    // Is required option   
     $ini_array[$key] = &quot;$option = $optionvalue\n&quot;;      // Yes save new value
     $ini_array[$key] = "$option = $optionvalue\n";      // Yes save new value
     file_put_contents(&quot;$file&quot;,$ini_array);              // Save array to file
     file_put_contents("$file",$ini_array);              // Save array to file
     return true;                                        // Success return true                           
     return true;                                        // Success return true                           
   }                                                   
   }                                                   
Line 200: Line 199:


function my_ini_get($file,$block,$option){
function my_ini_get($file,$block,$option){
  $ini_array = file(&quot;$file&quot;);                        // Read file into array
  $ini_array = file("$file");                        // Read file into array


  $flag=false;                                        // Set found flag
  $flag=false;                                        // Set found flag
  foreach($ini_array as $key =&gt; $value){              // Scan array line by line
  foreach($ini_array as $key => $value){              // Scan array line by line


   if($flag &amp;&amp; preg_match('/^\s*\[/',$value)){      // Is it a new block  
   if($flag &&  preg_match('/^\s*\[/',$value)){      // Is it a new block  
     $flag=false;                                    // Yes: Rest flag                                   
     $flag=false;                                    // Yes: Rest flag                                   
   }
   }


   if(trim($value) == &quot;[$block]&quot;){                  // Is it required block               
   if(trim($value) == "[$block]"){                  // Is it required block               
     $flag=true;                                    // Yes: set found flag         
     $flag=true;                                    // Yes: set found flag         
   }
   }


   if($flag &amp;&amp; preg_match(&quot;/^\s*$option/&quot;,$value)){    // Is required option   
   if($flag &&  preg_match("/^\s*$option/",$value)){    // Is required option   
     $str  =  trim($ini_array[$key]);                    // Yes read and clean  
     $str  =  trim($ini_array[$key]);                    // Yes read and clean  
     $str1 =  preg_replace(&quot;/^$option\s*=\s*/&quot;, &quot;&quot;, $str); // Cean  
     $str1 =  preg_replace("/^$option\s*=\s*/", "", $str); // Cean  
     return $str1;                                      // Retun option value  
     return $str1;                                      // Retun option value  
   }                                                   
   }                                                   
  }
  }
  return &quot;&quot;;                                              // Option not found  
  return "";                                              // Option not found  
}
}
//=== END my_ini_get ==========================================================
//=== END my_ini_get ==========================================================
?&gt;
?>
&lt;/pre&gt;
</pre>


'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''

Navigation menu