tasairis > thank you very much, it works just fine. I didn't quite understand the =& operator. In the php documentation it talks about making a clone but from what I understand its to move the pointer ??
Jonah Bron > yes, thanks! Here's how it goes:
I created a configuration file which is text only and more user friendly than putting actual php code in.
It's basically very simple, it allows to create "vars" in the following format:
[text]#var_name = content of that var[/text]
or with children :
[text]#var_name[child][child2] = content[/text]
I then create an array from that file using file() and then return an array with all the valid vars.
I know it take a lot more work for php to process that, but it is only used once or twice. Actually it's used for stuff like database connection information and such.
Just for fun, here's the code. Let me know if you see any faster and easier way to do it :
Code: Select all
// CREATE ARRAY FROM FILE
define('FILETOARRAY_NBR_ALLOWED',6);
function file_to_array($file)
{
$reg_is_var = '/^\t*\#(?P<name>[a-z_\.]+[0-9a-z_\.]*)'; // IS A VAR ?
for($i=1;$i<=FILETOARRAY_NBR_ALLOWED;$i++){
$reg_is_var .= '(\[(?P<key'.$i.'>[a-z_\.0-9]*)\])?'; // Keys
}
$reg_is_var .= '\s?\=\s?(?P<value>.+)$/'; // Value
$array = array();
foreach ($file as $line_num => $line) {
if(preg_match($reg_is_var,$line)){
preg_match($reg_is_var,$line,$rslt);
// check for key 1,2,3 and 4
$is_array = false; // assuming array keys are all empty
$tmp = array($rslt['name']);
for($i=1;$i<=FILETOARRAY_NBR_ALLOWED;$i++){
if(isset($rslt['key'.$i]) && !empty($rslt['key'.$i])){
$is_array = true;
array_push($tmp,$rslt['key'.$i]);
}else{
break;
}
}
if($is_array){
array_push($tmp,$rslt['value']);
$last = array_pop($tmp);
$new_array = array();
$current =& $new_array; // the furthest into $new_array
foreach ($tmp as $item) {
$current[$item] = array();
$current =& $current[$item];
}
$current = $last;
$array = array_merge_recursive($array,$new_array);
}else{
$array[$rslt['name']] = $rslt['value']; // Set default (kept if not an array)
}
}
}
return $array;
}
cheers !