Results 1 to 2 of 2

Thread: Simple PHP Configuration/Database Functions

  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.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

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