Results 1 to 2 of 2

Thread: Simple PHP Configuration/Database Functions

Threaded View

  1. #1

    Thread Starter
    Member Doomguy0505's Avatar
    Join Date
    Apr 2005
    Posts
    55

    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("&", "&amp;", $newdata);
    $newdata = str_replace(";", ";", $newdata);
    $newdata = str_replace(" ", "&nbsp;", $newdata);
    $newdata = str_replace(">", "&gt;", $newdata);
    $newdata = str_replace("<", "&lt;", $newdata);
    $newdata = str_replace("/", "&frasl;", $newdata);
    $newdata = str_replace("\n", "", $newdata);
    $newdata = str_replace("=", "=", $newdata);
    return $newdata;
    }
    Last edited by Doomguy0505; Jun 1st, 2006 at 07:25 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width