My Ini Design: Ini Get
My Ini Design: Introduction | Ini Set | Ini Get | Final Functions | parse_ini_file
|
Uniform Server Cron Configuration |
This page looks at proof of concept for a my_ini_get function.
Most of the groundwork has been completed on the previous page.
Requirement
Basic requirement is to target a specific configuration block and option, once targeted return that option's value.
For Example: Suppose we want to target block [drupal] and return the value for the option period.
|
UniServer\plugins\test\cron.ini ; Test example file for cron timers ; Period values hourly, daily or weekly [moodle] path = http://localhost/moodle/admin/cron.php period = daily ref = [drupal] path = http://localhost/drupal/cron.php period = daily ref = [dtdns] path = ..\..\plugins\dtdns_updater\dtdns_updater.php period = hourly ref = |
Hey! I have just read something similar on the previous page. Yep! That’s the whole point the two function are symmetrical. Instead of changing the array value we read it.
Example 4
Modify Test Script as shown on right: It's a small change, we print the value contained in the array:
What is displayed is raw data contained in the array. It clearly shows the need to |
UniServer\plugins\test\test.php <?php $ini_array = file("cron.ini"); $flag=false; foreach($ini_array as $key => $value){ if($flag && preg_match('/^\s*\[/',$value)){ $flag=false; } if(trim($value) == "[drupal]"){ $flag=true; } if($flag && preg_match('/^\s*period/',$value)){ print "####".$ini_array[$key]."####"; break; } } file_put_contents("out_cron.txt",$ini_array); ?> |
Clean Array Output
You can use several methods to clean the array output it just depends what you are comfortable with.
If you are cleaver and want to make the code hard to read do it in a single regx.
Alternatively break it down and do it in easy stages.
####period = daily#### Press any key to continue . . . |
|
Part of UniServer\plugins\test\test.php if($flag && preg_match('/^\s*period/',$value)){ $str = trim($ini_array[$key]); print "####".$str."####\n"; break; } |
The reference markers # show spaces and new line have been removed. This makes a regex easy to use. OK these things can be as difficult as you like to make them I just like simple.
What we want to eliminate is "period = " the "period" and "=" are well defined however what we do not know is how many spaces a user used.
Well scanning from left to right create a regex pattern:
^ | - Indicates start at the beginning of a line |
period | - That's the word we are looking for hence use as is |
\s* | - \s is a special code meaning a space, the * is another special code meaning 0 or more of the previous character. hence chects for any number of space. |
= | - A well defined character we are looking for. |
\s* | - As above. |
That completes what we are looking for (pattern). All that remains is to replace it with nothing "" (replacement).
####daily#### Press any key to continue . . . |
|
Part of UniServer\plugins\test\test.php if($flag && preg_match('/^\s*period/',$value)){ $str = trim($ini_array[$key]); $str1 = preg_replace('/^period\s*=\s*/', "", $str); print "####".$str1."####\n"; break; } |
Code
Final test code is shown on the right:
|
UniServer\plugins\test\test.php <?php $ini_array = file("cron.ini"); $flag=false; foreach($ini_array as $key => $value){ if($flag && preg_match('/^\s*\[/',$value)){ $flag=false; } if(trim($value) == "[drupal]"){ $flag=true; } if($flag && preg_match('/^\s*period/',$value)){ $str = trim($ini_array[$key]); $str1 = preg_replace('/^period\s*=\s*/', "", $str); print "####".$str1."####\n"; break; } } file_put_contents("out_cron.txt",$ini_array); ?> |
Summary
Above completes our test code for the second function.
The two snippets of code are proof of concept on the next page these are converted into functions.