Author Topic: Editing text files with PHP  (Read 1361 times)

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Editing text files with PHP
« on: August 07, 2007, 07:59:41 AM »
For my rpg, I want to store the game settings somewhere so that it can be edited again later, from an admin panel.
Before this, I used to just store the settings in the database, but I'm thinking of using a text file instead.
It helps in case the admin can't login or something, then they can just go edit a simple textfile rather than login to phpmyadmin.

The problem is, I don't know how to edit certain parts of a text file.
So I'm asking for some ideas on how to do this.
:)

There's not just gonna be one setting to edit, so there could be several changes to the text file at one time.

Should I find a way to edit certain lines of the text file? Or should I just rewrite the whole settings file each time the settings are changed?

Offline dvd871

  • Level 21
  • *
  • Posts: 238
  • Reputation: +7/-0
    • View Profile
    • Dominion Siege
Re: Editing text files with PHP
« Reply #1 on: August 08, 2007, 11:02:19 PM »
Well you could just read in all the contents of your config file and dump it into a textarea allowing edits that way. Pretty cheesey :)  I had the same thought that you did, but I finally just decided to make a config.php file with all my needed settings.  If a game admin can't edit that then they shouldn't be a game admin anyways.

Offline Dasein Fiasco

  • Level 15
  • *
  • Posts: 132
  • Reputation: +2/-0
    • View Profile
    • Travis Dunn
Re: Editing text files with PHP
« Reply #2 on: August 08, 2007, 11:10:13 PM »
Hopefully, you won't have cases where your admins can't log in. But if you're not using a database, consider storing data in XML instead of a .txt file. You can even impose a quasi-relational data structure on your XML data if you so desired. I'm sure there are lots of PHP XML libraries out there, and the markup rules aren't hard to learn if you're not already conversant in XML.
Personal Site: www.travisdunn.com
There is neither speech nor language but their voice is heard among them

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Editing text files with PHP
« Reply #3 on: August 09, 2007, 04:27:21 AM »
Hmm, but XML can be read by anybody as long as you know the filename, can't it?
dvd871, I'll use your suggestion if I can't get this to work :D I just hope they don't accidentally erase their game settings :P

I was thinking of something like the way SMF edits the forum settings, so I figured I'll just take a look into their source code anyways :P

Here's the function used to edit the settings file, I haven't looked through it yet but I'll try and see if I can understand it :)

// Update the Settings.php file.
function updateSettingsFile($config_vars)
{
	
global 
$boarddir;

	
// Load the file.  Break it up based on \r or \n, and then clean out extra characters.
	
$settingsArray file_get_contents($boarddir '/Settings.php');
	
if (
strpos($settingsArray"\n") !== false)
	
	
$settingsArray explode("\n"$settingsArray);
	
elseif (
strpos($settingsArray"\r") !== false)
	
	
$settingsArray explode("\r"$settingsArray);
	
else
	
	
return;

	
// Make sure we got a good file.
	
if (
count($config_vars) == && isset($config_vars['db_last_error']))
	
{
	
	
$temp trim(implode("\n"$settingsArray));
	
	
if (
substr($temp05) != '<?php' || substr($temp, -2) != '?' '>')
	
	
	
return;
	
	
if (
strpos($temp'sourcedir') === false || strpos($temp'boarddir') === false || strpos($temp'cookiename') === false)
	
	
	
return;
	
}

	
// Presumably, the file has to have stuff in it for this function to be called :P.
	
if (
count($settingsArray) < 10)
	
	
return;

	
foreach (
$settingsArray as $k => $dummy)
	
	
$settingsArray[$k] = strtr($dummy, array("\r" => '')) . "\n";

	
for (
$i 0$n count($settingsArray); $i $n$i++)
	
{
	
	
// Don't trim or bother with it if it's not a variable.
	
	
if (
substr($settingsArray[$i], 01) != '$')
	
	
	
continue;

	
	
$settingsArray[$i] = trim($settingsArray[$i]) . "\n";

	
	
// Look through the variables to set....
	
	
foreach (
$config_vars as $var => $val)
	
	
{
	
	
	
if (
strncasecmp($settingsArray[$i], '$' $varstrlen($var)) == 0)
	
	
	
{
	
	
	
	
$comment strstr(substr($settingsArray[$i], strpos($settingsArray[$i], ';')), '#');
	
	
	
	
$settingsArray[$i] = '$' $var ' = ' $val ';' . ($comment == '' '' "\t\t" rtrim($comment)) . "\n";

	
	
	
	
// This one's been 'used', so to speak.
	
	
	
	
unset(
$config_vars[$var]);
	
	
	
}
	
	
}

	
	
if (
substr(trim($settingsArray[$i]), 02) == '?' '>')
	
	
	
$end $i;
	
}

	
// This should never happen, but apparently it is happening.
	
if (empty(
$end) || $end 10)
	
	
$end count($settingsArray) - 1;

	
// Still more?  Add them at the end.
	
if (!empty(
$config_vars))
	
{
	
	
if (
trim($settingsArray[$end]) == '?' '>')
	
	
	
$settingsArray[$end++] = '';
	
	
else
	
	
	
$end++;

	
	
foreach (
$config_vars as $var => $val)
	
	
	
$settingsArray[$end++] = '$' $var ' = ' $val ';' "\n";
	
	
$settingsArray[$end] = '?' '>';
	
}
	
else
	
	
$settingsArray[$end] = trim($settingsArray[$end]);

	
// Sanity error checking: the file needs to be at least 12 lines.
	
if (
count($settingsArray) < 12)
	
	
return;

	
// Blank out the file - done to fix a oddity with some servers.
	
$fp = @fopen($boarddir '/Settings.php''w');

	
// Is it even writable, though?
	
if (
$fp)
	
{
	
	
fclose($fp);

	
	
$fp fopen($boarddir '/Settings.php''r+');
	
	
foreach (
$settingsArray as $line)
	
	
	
fwrite($fpstrtr($line"\r"''));
	
	
fclose($fp);
	
}
}

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Editing text files with PHP
« Reply #4 on: August 25, 2007, 07:48:18 AM »
Had the same problem with the Node Game Engine..

I wanted game master's to be able to edit there games, however, the config file also had master settings that were only available to myself.. As an example the Node Game Engine allows for plug and play modules the game master can add to there game to make it more unique. These are pre-built modules by myself but they can configured by the game master.

The config file would have the master setting
MODULE_XYZ = true

then the config setting which the game masters could set was something like
XYZ_max_attempts = 4

I ended up going with a quasi DB / text file approach... A config table would hold all the settings, values and security levels. So if I go to my admin panel I can turn off / on modules which would then change them in the database and then create and save all those settings to a config file that was saved. Same was true for the game master they would see the settings, can change them etc.. Once they hit save the entire table was read and dumped to a text file that the game code would use.



Creating online addictions, one game at a time:

Offline beam

  • Level 15
  • *
  • Posts: 132
  • Reputation: +2/-0
  • Dance Commander
    • View Profile
Re: Editing text files with PHP
« Reply #5 on: August 27, 2007, 03:54:08 PM »
I've used this for a few things. Of course, you have to manually edit the config file (but you should probably just make an admin interface with a textbox for that ;))

But anyway, here's my code:

Code: [Select]
// usage: array = load_settings(filename)
function load_settings($file) {
// store the settings values
$settings = array();

// split the file into lines
$lines = file($file);

// parse the file line by line
foreach ($lines as $line) {
// trim and get rid of comments
$uncommented_line = explode("#", trim($line));
$line = $uncommented_line[0];

// split the line by '='
$line_data = explode("=", trim($line), 2);

// translate line to settings
if (count($line_data) == 2) {
// clean up $line_data and store it
$settings[trim($line_data[0])] = trim($line_data[1]);
}
}

// return array of settings
return $settings;
}

Basically, the function just returns an array of all the configuration options. Lines that begin with # are comments (you can even add comments after config rules).

Here's my debugging config file:

Code: [Select]
# this is the test config
EASY_SCRIPT = true
BEAM_RULES = true

# i hope it works
SCRIPT_FAILS = false

# comment test
COMMENTS = true # invisible comment

Enjoy dude.

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Editing text files with PHP
« Reply #6 on: October 03, 2007, 11:41:03 AM »
I've still got this problem, but I'm posting this to remind myself and it's also interesting.

I found a function called parse_ini_file:
http://www.php.net/manual/en/function.parse-ini-file.php

It looks very useful, even though it's not that much different from a regular PHP config file.
A lot of people have posted their own functions on writing/reading INI files, and it looks like they work.

What do you think?

Offline beam

  • Level 15
  • *
  • Posts: 132
  • Reputation: +2/-0
  • Dance Commander
    • View Profile
Re: Editing text files with PHP
« Reply #7 on: October 04, 2007, 05:09:58 AM »
Now all you need is a function to write to the file from an array.

fake edit: seems like people have done that in the comments on the php.net page

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal