note 118331 added to function.dba-replace
| From: | domokosendrejanos | Date: | Wed, 18 Nov 2015 20:24:01 +0000 |
| Subject: | note 118331 added to function.dba-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204177@lists.php.net to get a copy of this message | ||
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");
----
Server IP: 72.52.91.14
Probable Submitter: 89.135.95.78
----
Manual Page -- http://php.net/manual/en/function.dba-replace.php
Edit -- https://master.php.net/note/edit/118331
Del: integrated -- https://master.php.net/note/delete/118331/integrated
Del: useless -- https://master.php.net/note/delete/118331/useless
Del: bad code -- https://master.php.net/note/delete/118331/bad+code
Del: spam -- https://master.php.net/note/delete/118331/spam
Del: non-english -- https://master.php.net/note/delete/118331/non-english
Del: in docs -- https://master.php.net/note/delete/118331/in+docs
Del: other reasons-- https://master.php.net/note/delete/118331
Reject -- https://master.php.net/note/reject/118331
Search -- https://master.php.net/manage/user-notes.php