|
-
Jun 26th, 2005, 03:14 AM
#1
Thread Starter
Banned
Ini #2
Fuction
PHP Code:
<?php
//****************************************************************************************
//INIFunc.php
//Freeware functions
//Copyright 2001 Nicola Viganò <[email protected]>
//THIS CODE IS DISTRIBUITED "AS IT IS", WITHOUT WARRANTY
//****************************************************************************************
//Instructions
//
//Functions use any of these parameters:
// $section : section name
// $key(s) : key(s) name
// $value : value
// $default : default value if $key can not be found
// $file_name : name of the file
//
//Available functions:
//
// write_key($section, $key, $value, $file_name)
// Writes a key and its value under a certain section.
// It creates the file if it doesn't exist.
//
// write_section($section, $keys, $file_name)
// Writes an entire section and its keys.
// It creates the file if it doesn't exist.
// $keys must be a single dimension array, structured as: $keys[$key]=$value.
// eg: $keys["Key1"]="Value1";
// $keys["Key2"]="Value2";
// ...
// eg: $keys = array( "Key1" => "Value1", "Key2" => "Value2");
//
// delete_key($section, $key, $file_name)
// Deletes a key
//
// delete_section($section, $file_name)
// Deletes an entire section.
//
// read_key($section, $key, $default, $file_name)
// Reads a certain value.
// It returns $default if the value or the file do not exist.
//
// read_section($section, $default, $file_name)
// Reads an entire section.
// It returns a single dimension array, structured as: $keys[$key]=$value.
// It returns $default if the section or the file do not exist.
// eg: If $keys is the returned value, you can show values with the following code:
// foreach ($keys as $key=>$value)
// echo "$key => $value<br>";
function write_key($section, $key, $value, $file_name)
{
if (!file_exists($file_name))
{ //File not found
$content = "[$section]\n";
$content .= "$key=$value";
}
else
{
$content = read_ini($file_name);
//Find section
$currentsection = find_section($content, $section, FALSE);
if (!isset($currentsection))
{
//Section not found
$content .= "\n\n[$section]\n";
$content .= "$key=$value";
}
else
{
//Find key
$currentkey = find_key($currentsection, $key, FALSE);
if (!isset($currentkey))
{ //Key not found
$newsection = $currentsection."\n$key=$value";
$content = str_replace($currentsection, $newsection, $content);
}
else
{
$newsection = str_replace($currentkey, "$key=$value", $currentsection);
$content = str_replace($currentsection, $newsection, $content);
}
}
}
write_ini ($content, $file_name);
}
function write_section($section, $keys, $file_name)
{
if (!file_exists($file_name))
{ //File not found
$content = "[$section]\n";
foreach ($keys as $key=>$value)
$content .="$key=$value\n";
}
else
{
$content = read_ini($file_name);
//Find section
$currentsection = find_section($content, $section, FALSE);
if (!isset($currentsection))
{
//Section not found
$content .= "\n\n[$section]\n";
foreach ($keys as $key=>$value)
$content .="$key=$value\n";
}
else
{
$newsection = $currentsection;
//Find key
foreach ($keys as $key=>$value)
{
$currentkey = find_key($currentsection, $key, FALSE);
if (!isset($currentkey))
{ //Key not found
$newsection .= "\n$key=$value";
}
else
{
$newsection = str_replace($currentkey, "$key=$value", $newsection);
}
}
$content = str_replace($currentsection, $newsection, $content);
}
}
write_ini ($content, $file_name);
}
function delete_key($section, $key, $file_name)
{
if (!file_exists($file_name))
return (FALSE);
$content = read_ini($file_name);
//Find section
$currentsection = find_section($content, $section, FALSE);
if (!isset($currentsection))
return (FALSE);
//Find key
$currentkey = find_key($currentsection, $key, FALSE);
if (!isset($currentkey))
return (FALSE);
$newsection = str_replace("\n".$currentkey, "", $currentsection);
$content = str_replace($currentsection, $newsection, $content);
write_ini ($content, $file_name);
return (TRUE);
}
function delete_section($section, $file_name)
{
if (!file_exists($file_name))
return (FALSE);
$content = read_ini($file_name);
//Find section
$currentsection = find_section($content, $section, FALSE);
if (!isset($currentsection))
return (FALSE);
$content = str_replace($currentsection, "", $content);
write_ini ($content, $file_name);
return (TRUE);
}
function read_key($section, $key, $default, $file_name)
{
if (!file_exists($file_name))
return($default);
$content = read_ini($file_name);
//Find section
$currentsection = find_section($content, $section, TRUE);
if (!isset($currentsection))
return($default);
//Find key
$value = find_key($currentsection, $key, TRUE);
if (!isset($value))
return($default);
return ($value);
}
function read_section($section, $default, $file_name)
{
if (!file_exists($file_name))
return($default);
$content = read_ini($file_name);
//Find section
$currentsection = find_section($content, $section, TRUE);
if (!isset($currentsection))
return($default);
//Get keys list
$lines = explode("\n" , $currentsection);
foreach ($lines as $line)
{
list($key,$value)=explode('=',$line);
$keys[$key]=$value;
}
return ($keys);
}
//************************************************************************************
//Private functions
//************************************************************************************
function read_ini ($file_name)
{
clearstatcache();
$file = fopen($file_name, "r");
flock($file, LOCK_SH);
$content = trim(@fead($file, filesize($file_name)));
flock($file, LOCK_UN);
fclose ($file);
return ($content);
}
function write_ini ($content, $file_name)
{
$file = fopen($file_name, "w");
flock($file, LOCK_EX);
fwrite($file, $content);
flock($file, LOCK_UN);
fclose ($file);
}
function find_section ($content, $section, $just_keys)
{
$section = "[$section]\n";
$currentsection = strstr($content, $section);
if (!$currentsection)
return($empty);
$endpos = strpos($currentsection, "\n\n");
if (!$endpos)
$endpos = strpos($currentsection, "\n[");
if (!$endpos)
$endpos = strlen($currentsection);
if ($just_keys)
$currentsection = trim(substr($currentsection, strlen($section), $endpos-strlen($section)));
else
$currentsection = trim(substr($currentsection, 0, $endpos));
return ($currentsection);
}
function find_key ($currentsection, $key, $just_value)
{
$key .= "=";
$currentkey = strstr($currentsection, $key);
if (!$currentkey)
return($empty);
$endpos = strpos($currentkey, "\n");
if (!$endpos)
$endpos = strlen($currentkey);
if ($just_value)
$currentkey = trim(substr($currentkey, strlen($key), $endpos-strlen($key)));
else
$currentkey = trim(substr($currentkey, 0, $endpos));
return ($currentkey);
}
?>
My code
PHP Code:
<?
include("inifunc.php");
$value = read_key("Index", "Loads", "default value", "test.txt");
PRINT("$value");
?>
test.txt
Problem:
Nothing happens!
-
Jun 26th, 2005, 06:23 PM
#2
Fanatic Member
Re: Ini #2
ahem... are you forgetting some "squiggly brackets"?
PHP Code:
function read_key($section, $key, $default, $file_name)
{
if (!file_exists($file_name))
return($default);
$content = read_ini($file_name);
look at the if... are you missing something?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|