Web Hosting Talk







View Full Version : PHP: String Manipulation


UrlGuy
05-04-2005, 06:16 PM
Hi,

I have this PHP script which I want to allow my users to change its configuration settings.

The config.php file could look something like this

<?PHP
$colour = "blue";
$food = "pizza";
?>

What I want to do is, I want to make a HTML or PHP form, where I can allow my users to edit the value of my different variables.

The variables and their values are already assigned, so I would need to edit the current ones.

Lets say I wanted to edit the above variable $colour's value from blue to red, without changing the variable or its name itself, only its value.

Like.. search and replace or something like that.


Background info:
I want to make a form for the users, kinda like where they access their "account page", and can edit their own account settings. Such as recieve emails, enable/disable settings and such. The variables are already set so I need only to change their value, without knowing what their value is in advance.


Anyone who can help me get started?


Thanks. :-)

zupanm
05-04-2005, 06:44 PM
one easy way to do this if you can use like a mysql DB is set one up for that.. or you can use a pear package called Config

http://pear.php.net/package/Config

innova
05-04-2005, 07:03 PM
This is the kind of thing that databases are made for.

Why have config files 'written' in a scripting language?

I would either use mysql (preferred) or sqlite if you want an embedded-DB in a single file.

smo123
05-04-2005, 10:15 PM
Database is better choice. But if you want to use file to store the data then here is the code. You have to write the values in a file. Every time you have to write the variable with the value taken from a form. Here is the code to write to file

if(@$status=="update"){
$configfile="<"."?php

\$bdcolor=\"$bdcolor\";
\$page_bg_color=\"$page_bg_color\";
\$page_text_color=\"$page_text_color\";

?".">";

$filenum=fopen("../include/config_display.php","w");
fwrite($filenum,$configfile);
fclose($filenum);
echo"<center> Entered data to config_display.php </center>" ;
}

You can collect the values from a form and the present value of the variable you can display like this .
<input type=text name='page_bg_color' value='$page_bg_color' size='8'>

With this you can write the values to the variables.

UrlGuy
05-06-2005, 09:28 AM
But I dont want to use a database with this script.

I have a config file with a few variables, I want to be able to change those variables' values without changing the variable name. Kinda like search and edit.

zupanm
05-06-2005, 11:13 AM
then use what i showed you

http://pear.php.net/package/Config