note 46679 deleted from function.parse-ini-file by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 17:16:53 +0000 |
| Subject: | note 46679 deleted from function.parse-ini-file by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176176@lists.php.net to get a copy of this message | ||
Note Submitter: Nick Deppe
----
I just noticed that the code I wrote before had an error in it. I have the fix posted here:
That is what happens when you don't error check the code first. Duh.
Here is yet another version of write_ini_file. This version takes data types into account. If the
file is numeric or boolean, the value is written in the ini file without quotes. Else it will be
written with quotes.
Please note that if a string that CAN be converted into a number WILL be converted into a number
because I used the is_numeric function. If you want to make sure that the data type is strictly
preserved, use the is_integer and is_double functions in place of the is_numeric function.
<?php
if(!function_exists('write_ini_file')) {
function write_ini_file($path, $assoc_array) {
foreach($assoc_array as $key => $item) {
if(is_array($item)) {
$content .= "\n[{$key}]\n";
foreach ($item as $key2 => $item2) {
if(is_numeric($item2) || is_bool($item2))
$content .= "{$key2} = {$item2}\n";
else
$content .= "{$key2} = \"{$item2}\"\n";
}
} else {
if(is_numeric($item) || is_bool($item))
$content .= "{$key} = {$item}\n";
else
$content .= "{$key} = \"{$item}\"\n";
}
}
if(!$handle = fopen($path, 'w')) {
return false;
}
if(!fwrite($handle, $content)) {
return false;
}
fclose($handle);
return true;
}
}
?>