Re: [PHP] storing an array in an ini file
Hi, had the same problem before and got around this by writing my own tiny parser that should work just nicely ( as serializing is a pain-in-the-ass when editing the .ini file on foot ) Anyway i switched to using an XML-File now :-) define ('REGEXP_VARIABLE','[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'); define ('REGEXP_INDEX','[0-9a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'); /** * @author Christian Jerono [EMAIL PROTECTED] * @paramstring $file_name Zu verarbeitende .INI-Datei * @parambool$process_sections Verarbeitung von Sectionen * @return array */ function _parse_config_file( $file_name , $process_sections = FALSE ){ $match_index = #^\[?(. REGEXP_INDEX .)\]?#; $match_section = #^\s*\[(. REGEXP_VARIABLE .)\]#; $match_config_param = #^\s*(. REGEXP_VARIABLE .(\[. REGEXP_INDEX .\])*)\s*=\s*(.*)$#; $match_value = #^(([\\\'])(.*)(?(2)[\\\']))|([^;]*).*$#; $return = array(); $file_content = file($file_name); $pointer = $return; foreach ( $file_content AS $file_line ){ $file_line = trim($file_line); if ( $process_sections === TRUE AND preg_match( $match_section , $file_line , $section ) ){ $pointer = $return[$section[1]]; } else if ( preg_match( $match_config_param , $file_line , $config_param ) ) { $index_name = $config_param[1]; $value = trim(preg_replace( $match_value , \\3\\4 , $config_param[3] )); $sub_pointer = $pointer; while ( preg_match( $match_index , $index_name , $temp ) ){ $sub_pointer = $sub_pointer[$temp[1]]; $index_name = substr($index_name,strlen($temp[0])); } $sub_pointer = defined($value) ? constant($value) : $value; } } return $return; } -- red [...] I have an ini file for my application and I use parse_ini_file to load it and some custom code to save modifications. I'd like to figure out how to store array values in there since that is the logical place to keep config data. If I can't, I could probably use a string with @@ or | in it as the token but an array is so much more elegant because that's what it's meant to do :-) Any suggestions? [...] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] storing an array in an ini file
Red, et al -- ...and then Red Wingate said... % % Hi, % % had the same problem before and got around this by writing my own tiny % parser that should work just nicely ( as serializing is a pain-in-the-ass when % editing the .ini file on foot ) Indeed :-) It looks quite interesting; thanks! % % Anyway i switched to using an XML-File now :-) One of these days... Baby steps :-) Thanks HAND :-D -- David T-G [EMAIL PROTECTED] http://justpickone.org/davidtg/ Shpx gur Pbzzhavpngvbaf Qrprapl Npg! pgp0.pgp Description: PGP signature
[PHP] storing an array in an ini file
Hi, all -- I have an ini file for my application and I use parse_ini_file to load it and some custom code to save modifications. I'd like to figure out how to store array values in there since that is the logical place to keep config data. If I can't, I could probably use a string with @@ or | in it as the token but an array is so much more elegant because that's what it's meant to do :-) Any suggestions? TIA HAND :-D -- David T-G [EMAIL PROTECTED] http://justpickone.org/davidtg/ Shpx gur Pbzzhavpngvbaf Qrprapl Npg! pgp0.pgp Description: PGP signature
Re: [PHP] storing an array in an ini file
* Thus wrote David T-G ([EMAIL PROTECTED]): Hi, all -- I have an ini file for my application and I use parse_ini_file to load it and some custom code to save modifications. I'd like to figure out how to store array values in there since that is the logical place to keep config data. If I can't, I could probably use a string with @@ or | in it as the token but an array is so much more elegant because that's what it's meant to do :-) You could always serialize the array to a value. and unserialize it after the call to parse_ini_file(). Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] storing an array in an ini file
Curt, et al -- ...and then Curt Zirzow said... % % * Thus wrote David T-G ([EMAIL PROTECTED]): % % I have an ini file for my application and I use parse_ini_file to load it % and some custom code to save modifications. I'd like to figure out how % to store array values in there since that is the logical place to keep ... % % You could always serialize the array to a value. and unserialize it % after the call to parse_ini_file(). I thought about that, but what I'm really after is not having to do any special processing for a variable -- both because it makes special cases and because then I'd have to reserialize it if I were to write out a change. Of course, I may be stuck at that point, but I'd sure love to see how I can store it natively just like I can declare it natively in a script. % % Curt Thanks HAND :-D -- David T-G [EMAIL PROTECTED] http://justpickone.org/davidtg/ Shpx gur Pbzzhavpngvbaf Qrprapl Npg! pgp0.pgp Description: PGP signature
Re: [PHP] storing an array in an ini file
* Thus wrote David T-G ([EMAIL PROTECTED]): Curt, et al -- ...and then Curt Zirzow said... % % * Thus wrote David T-G ([EMAIL PROTECTED]): % % I have an ini file for my application and I use parse_ini_file to load it % and some custom code to save modifications. I'd like to figure out how % to store array values in there since that is the logical place to keep ... % % You could always serialize the array to a value. and unserialize it % after the call to parse_ini_file(). I thought about that, but what I'm really after is not having to do any special processing for a variable -- both because it makes special cases and because then I'd have to reserialize it if I were to write out a change. My other though, which I probably should have mentioned, was to patch php's ini handling to allow for something like [Section] var[1]=element1 var[2]=element2 ... Thus the results would be something like Array ( [Section] = Array ( var = Array ( [1] = element1 [2] = element2 ) ) ) I dont know if [] characters are valid in keys Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] storing an array in an ini file
David T-G wrote: I have an ini file for my application and I use parse_ini_file to load it and some custom code to save modifications. I'd like to figure out how to store array values in there since that is the logical place to keep config data. If I can't, I could probably use a string with @@ or | in it as the token but an array is so much more elegant because that's what it's meant to do :-) If you really need to define arrays, just put them in a .php file and include() it. You're just going to use some hack to do it with an .ini file and parse_ini_file(), so why bother? ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] storing an array in an ini file
John, et al -- ...and then John Holmes said... % % David T-G wrote: % % to store array values in there since that is the logical place to keep % config data. If I can't, I could probably use a string with @@ or | in ... % % If you really need to define arrays, just put them in a .php file and % include() it. You're just going to use some hack to do it with an .ini % file and parse_ini_file(), so why bother? 1) I had hoped to not have to hack :-) 2) Another file that has to be writable by the web server is a pain 3) If I'm going to code a special case then I might as well do it in the same file as all of the other configs 4) It would save me having to write yet another save-the-changes mechanism (I kinda like what I have for the ini file; it worked out well and has handled everything so far :-) % % ---John Holmes... Enough already. I'll serialize any array and then handle it as a special case. Thanks to all for the help :-) HAND :-D -- David T-G [EMAIL PROTECTED] http://justpickone.org/davidtg/ Shpx gur Pbzzhavpngvbaf Qrprapl Npg! pgp0.pgp Description: PGP signature