note 65051 deleted from ref.array by tularis
| From: | tularis@php.net | Date: | Mon, 24 Apr 2006 13:58:08 +0000 |
| Subject: | note 65051 deleted from ref.array by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-109442@lists.php.net to get a copy of this message | ||
Note Submitter: matt0505 at bigpond dot net dot au
Reason: useless
----
Here is a configuration system made with arrays:
config_load(string data)
Returns an array [0][x] is the data and [1] is the count and 'data' is the
configuration text
config_add(array config, string value)
Adds an entry in config with the value value
config_remove(array config, string index)
Removes the entry index in config
config_set(array config, string index, string value)
Assigns the entry index inside config to the value
config_save(array config)
Returns the configuration text for config
Escape Sequences (Required for config)
string_escaped(string data)
Returns a string that has an escape sequence ("|" = "\p", linefeed =
"\n",
carriage = "\r", tab = "\t")
string_unescaped(string data)
Returns a string to the way it was before being escaped (blah\\ = blah\)
Format
data 0|data 1
All data is escaped
\/ Code Below \/
<?php
function config_remove($config, $index)
{
unset($config[0][$index]);
$config[1] = $config[1] - 1;
}
function config_add($config, $value)
{
$config[1]++;
$config[0][$config[1]] = $value;
}
function config_set($config, $index, $value)
{
$config[0][$index] = $value;
}
function config_save($config)
{
$i = 0;
$newdata = "";
for($i=0;$i<=$config[1];$i++)
{
$newdata = $newdata . string_escaped($config[0][$i]);
if ($i != $config[1])
{
$newdata = $newdata . "|";
}
}
return $newdata;
}
function config_load($data)
{
$newdata[0] = explode("|", $data);
$i = 0;
for($i=0;$i<=substr_count($data, "|");$i++)
{
$newdata[0][$i] = string_unescaped($newdata[0][$i]);
}
$newdata[1] = substr_count($data, "|");
return $newdata;
}
function string_escaped($value)
{
$bm = 0;
$newstring = $value;
$newstring = str_replace("\\", "\\\\", $newstring);
$newstring = str_replace("\t", "\\t", $newstring);
$newstring = str_replace("\n", "\\n", $newstring);
$newstring = str_replace("\r", "\\r", $newstring);
$newstring = str_replace("|", "\p", $newstring);
return $newstring;
}
function string_unescaped($value)
{
$i = 0;
$newstring = str_split($value);
$newvalue = "";
$backslash = 0;
for($i=0;$i<=strlen($value)-1;$i++)
{
if ($backslash == 1)
{
switch($newstring[$i])
{
case "\\":
$newvalue = $newvalue . "\\";
break;
case "r":
$newvalue = $newvalue . "\r";
break;
case "n":
$newvalue = $newvalue . "\n";
break;
case "t":
$newvalue = $newvalue . "\t";
break;
case "p":
$newvalue = $newvalue . "|";
break;
case '"':
$newvalue = $newvalue . '"';
break:
}
$backslash = 0;
$bm = 1;
}
if ($newstring[$i] == "\\")
{
if ($bm == 0)
{
$backslash = 1;
$bm = 1;
}
}
if ($bm == 0)
{
$newvalue = $newvalue . $newstring[$i];
}
$bm = 0;
}
return $newvalue;
}
?>