friky.com
← Back to archive

Post

How to emulate parse_ini_string in PHP5.2

2009-05-20 Agustí Pons Original post


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 !  
  

*   [parse\_ini\_file manual](http://www.php.net/manual/en/function.parse-ini-file.php)
*   [parse\_ini\_string manual](http://www.php.net/manual/en/function.parse-ini-string.php)

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