15.01.2010, 21:53
okay, took a little longer then i thought it would, but thats alright.
there's probably a more efficent method to do this, but i suppose this will do for now
let me give you a quick rundown of how it works.
set requires 3 inputs, the current data in your file, you have it defined as $data, it requires a key and a value ( like dini ). the key is what it searches for, the value is what it updates it with. When it's done it returns an updated version of the whole file, or whatever data was.
Heres an example on how to use it
get requires 2 values, data and the key. It will search for the key and return its corresponding VALUE, or false if it can't find it.
example
hope it helps, and let me know if something doesnt work, i checked it a couple of times but im tired as shit today so i may have left an error out somewhere
there's probably a more efficent method to do this, but i suppose this will do for now
PHP код:
function set($data, $key, $value) {
$regex = "/^$key+.+\n/";
$data = preg_replace($regex, "$key=$value\r\n", $data);
return $data;
}
function get($data, $key) {
$data = explode("\n", $data);
$regex = "/$key+=+.+$/";
$f = "";
for($i = 0; $i < count($data); $i++) {
if(preg_match($regex, $data[$i])) {
$f = $data[$i];
break;
}
}
if(is_array($f))
return false;
else {
$regex = "/$key+=/";
$f = preg_replace($regex, '', $f); //comment this line if you would like it to return KEY=VALUE instead of just VALUE
return $f;
}
}
set requires 3 inputs, the current data in your file, you have it defined as $data, it requires a key and a value ( like dini ). the key is what it searches for, the value is what it updates it with. When it's done it returns an updated version of the whole file, or whatever data was.
Heres an example on how to use it
PHP код:
if($data)
{
$data = set($data, "parameter1", 22);
$data = set($data, "parameter3", 62);
fwrite($handle, $data);
}
example
PHP код:
if($data)
{
echo get($data, "parameter1");
}