note 46672 deleted from function.parse-ini-file by irchtml
| From: | irchtml@php.net | Date: | Sun, 19 Dec 2004 09:35:38 +0000 |
| Subject: | note 46672 deleted from function.parse-ini-file by irchtml | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-82013@lists.php.net to get a copy of this message | ||
Note Submitter: Nick Deppe
----
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 .= "{$key2} = {$item2}\n";
else
$content .= "{$key2} = \"{$item2}\"\n";
}
}
if(!$handle = fopen($path, 'w')) {
return false;
}
if(!fwrite($handle, $content)) {
return false;
}
fclose($handle);
return true;
}
}
?>