Sorry for my very bad english. The dba_replace function not working (example: http://prog.hu/tudastar/189455/dba-replace-problema & http://dev.developer-works.com/article/9019279/%5BPHP5%5D+DBA+function+error+when+writing+ini+file+with+groups). This code works (but only on inifiles):
function dba_update($key, $value, $dbfileurl) {
if(file_exists($dbfileurl)) {
$key = trim($key);
$value = trim($value);
$index = explode("]", $key);
$index[0] = str_replace("[", "", $index[0]);
$dbarray = parse_ini_file($dbfileurl, true);
if(count($index) > 1) {
if(in_array($value, $dbarray)) { return FALSE; } elseif(isset($dbarray[$index[0]][$index[1]])) {
$dbarray[$index[0]][$index[1]] = $value;
$result = "";
foreach ($dbarray as $row_name => $row_value) {
$result .= "\n[$row_name]\n\n";
foreach ($row_value as $column_name => $column_value) {
if(is_numeric($column_value)) { $result .= "$column_name = $column_value\n\n"; }
else { $result .= "$column_name = '".$column_value."'\n\n"; }
}
}
}
} else {
if(in_array($value, $dbarray)) { return FALSE; } elseif(isset($key)) {
$dbarray[$key] = $value;
$result = "";
foreach ($dbarray as $key_name => $key_value) {
if(is_numeric($key_value)) { $result .= "$key_name = $key_value\n\n"; }
else { $result .= "$key_name = '".$key_value."'\n\n"; }
}
}
}
file_put_contents($dbfileurl, $result);
} else { return FALSE; }
return TRUE;
}
Calling the function:
dba_update("[$section]$key", $value, "example/dir/content.ini");
or (one dimension array):
dba_update($key, $value, "example/dir/content.ini");