Simple PHP Configuration/Database Functions
These are an alternative to databases and are more effective than ini files because they can have newlines and carriage returns.
EDIT: Reading bug fixed
Here's the code
Code:
<?php
function Escaped($escapedata)
{
$tempdata = $escapedata;
$tempdata = str_replace("\\", "\\\\", $tempdata);
$tempdata = str_replace("\n", "\\n", $tempdata);
$tempdata = str_replace("\r", "\\r", $tempdata);
$tempdata = str_replace("\t", "\\t", $tempdata);
return $tempdata;
}
function UnEscaped($escapedvalue)
{
$linedata = str_split($escapedvalue);
$curchar = "";
$escapedmode = 0;
$cnt = count($linedata);
$alldata = "";
for($i=0;$i<=$cnt;$i++)
{
$curchar = $linedata[$i];
if ($escapedmode == 1)
{
switch ($curchar)
{
case "\\":
$alldata = $alldata . "\\";
break;
case "n":
$alldata = $alldata . "\n";
break;
case "r":
$alldata = $alldata . "\r";
break;
case "t":
$alldata = $alldata . "\n";
break;
}
$escapedmode = 0;
}
else
{
if ($curchar == "\\") { $escapedmode = 1; continue; }
$alldata = $alldata . $curchar;
}
}
return $alldata;
}
function Cfg_GetKey($data, $ind, $subind)
{
$linedata = split("\n", $data);
$specificline = $linedata[$ind];
$splitdata = split("\t", $specificline);
$rldata = UnEscaped($splitdata[$subind]);
return $rldata;
}
function Cfg_WriteKey($data, $ind, $subind, $setdata)
{
$linedata = split("\n", $data);
$specificline = $linedata[$ind];
$splitdata = split("\t", $specificline);
$splitdata[$subind] = Escaped($setdata);
$nld = implode("\t", $splitdata);
$linedata[$ind] = $nld;
$retval = implode("\n", $linedata);
return $retval;
}
function Cfg_IndCount($data)
{
$dat = split("\n", $data);
return count($dat);
}
?>
Here's a useful function so that the user doesn't reformat your web page with comments/input.
Converts text writing to html formatting.
Code:
function CHTML($data)
{
$newdata = $data;
$newdata = str_replace("&", "&", $newdata);
$newdata = str_replace(";", ";", $newdata);
$newdata = str_replace(" ", " ", $newdata);
$newdata = str_replace(">", ">", $newdata);
$newdata = str_replace("<", "<", $newdata);
$newdata = str_replace("/", "⁄", $newdata);
$newdata = str_replace("\n", "", $newdata);
$newdata = str_replace("=", "=", $newdata);
return $newdata;
}
Re: Simple PHP Configuration/Database Functions
The htmlspecialchars() and htmlentities() function do exactly the same as your second example.
Prehaps you should add some phpdoc comments to the first peice of code so everyone knows what it does. :)