note 71545 deleted from function.parse-ini-file by bjori
| From: | bjori@php.net | Date: | Tue, 05 Dec 2006 15:08:06 +0000 |
| Subject: | note 71545 deleted from function.parse-ini-file by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-120304@lists.php.net to get a copy of this message | ||
Note Submitter: federico at nextware dot it
Reason: bad code
----
I have add to previus function write_ini_file some controls to verify the correct type of item,if
the item is null now write the reserved word null and if the item type is boolean write the value
true or false.
function write_ini_file($path, $assoc_array){
$content = '';
$sections = '';
foreach ($assoc_array as $key => $item) {
if (is_array($item)) {
$sections .= "\n[{$key}]\n";
foreach ($item as $key2 => $item2) {
if(is_null($item2)) {
$sections .= "{$key2} = null\n";
} elseif ( (strtoupper(trim($item2))=="TRUE") ||
(strtoupper(trim($item2))=="FALSE") ) {
$sections .= "{$key2} = {$item2}\n";
} elseif (is_bool($item2)) {
if ($item2) { $sections .= "{$key2} = true\n"; } else { $sections .= "{$key2}
= false\n"; }
} elseif (is_numeric($item2) || is_bool($item2)) {
$sections .= "{$key2} = {$item2}\n";
} else {
$sections .= "{$key2} = \"{$item2}\"\n";
}
}
} else {
if(is_null($item)) {
$content .= "{$key} = null\n";
} elseif ( (strtoupper($item)=="TRUE") || (strtoupper($item)=="TRUE") ) {
$content .= "{$key} = {$item}\n";
} elseif (is_bool($item)) {
if ($item2) { $sections .= "{$key} = true\n"; } else { $sections .= "{$key} =
false\n"; }
} elseif(is_numeric($item) || is_bool($item)) {
$content .= "{$key} = {$item}\n";
} else {
$content .= "{$key} = \"{$item}\"\n";
}
}
}
$content .= $sections;
if (!$handle = fopen($path, 'w'))
{
return false;
}
if (!fwrite($handle, $content))
{
return false;
}
fclose($handle);
return true;
}