20090520

How to emulate parse_ini_string in PHP5.2


If you have ever wanted to parse a ini file from "not-a-file" you may have
found the parse_ini_string function, but it only comes in PHP version 5.3 or newer.

Here you have a piece of code that has two options, the easy one, if parse_ini_string is present, use it, if not it creates a temporary file, put some content on it, parse_ini_file on that file and then delete the file.



if (function_exists('parse_ini_string'))
{// php 5.3
$return_ini_array=parse_ini_string($ini_file_string);
}
else
{// php anterior a 5.3
$temp_file_name = tempnam('/tmp', 'temp_lang_');
$handle = fopen($temp_file_name, "w");
fwrite($handle, $ini_file_string);
fclose($handle);
$return_ini_array=parse_ini_file($temp_file_name);
unlink($temp_file_name);
}



It gets an string with the ini-file-format called $ini_file_string and returns an array with the parsed elemements of the input string ($return_ini_array).

We'll that's not perfect, but it works !



PD: Com desestressa programar, algu vol anar a visitar clients mentre jo em quedo programant a la oficina ?


:)