note 46679 added to function.parse-ini-file

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

« previous php.notes (#78800) next »