note 46672 added to function.parse-ini-file
| From: | NickDeppe at osu1 dot php dot net | Date: | Tue, 19 Oct 2004 20:49:13 +0000 |
| Subject: | note 46672 added to function.parse-ini-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78789@lists.php.net to get a copy of this message | ||
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;
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.parse-ini-file.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46672
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46672&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46672&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46672&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46672&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46672&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46672&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46672&report=yes
Search -- http://master.php.net/manage/user-notes.php