View Full Version : How to edit certain lines in the file?

01-20-2008, 11:29 AM
As I said in title...

For example, I have a file settings.php. there's:

some code
$settings['title'] = "Blablabla";
some code

How to edit that "Blablabla" value? or that line?

01-20-2008, 11:36 AM
1)Open the file.
2)Read the contents to a buffer
3)Use some regex or simple string replace function to make the changes.
4)Write it back to file.

01-20-2008, 04:06 PM
If all of the settings are in the $settings array(i.e. you don't have any other arrays in the file like $dbsettings) you could change the settings in the script and generate a new file:

$settings = array('title' => 'test', 'setting1' => 'on'); // sample data, you won't need this

$temp = '<?php


foreach($settings as $k => $v)
$temp .= '$settings[\'' . $k . '\'] = \'' . addslashes($v) . '\';


$temp .= '


file_put_contents('config.php', $temp);

Edit: The vB php code syntax highlighter removes newlines and screws up the highlighting when you have <?php tags in a string, so I changed the strings and used code tags. You can see the original code here (http://pastebin.com/f46b2958d).

02-03-2008, 04:17 PM
I was trying hard, but I was unable to do it :(

I want to make admin panel where I can update site settings (with inputs).
and I want to display value of each setting in input too.
Here's the code:

if( 'settings.php' == basename($_SERVER['SCRIPT_FILENAME'])){
die ('<center><b>Error 404</b></center>');
$settings['slogan'] = "Learn More...";
$settings['charset'] = "UTF-8";
$settings['description'] = "Free online tutorials";
$settings['mod_rewrite'] = "0";
$settings['max'] = "3";
$settings['base'] = "http://localhost/cats/";
$settings['imgDir'] = "http://localhost/cats/images/";

and when I change language (in dropdown box) I want the script to write include_once('lang/somelang.php');

02-04-2008, 02:59 PM
Well, I was able to fix writing to the file (thanks to Inigoesdr) but I still don't know how to display those setting values in the input box. Any ideas?

02-04-2008, 08:31 PM
Presumably the current config file would be included so you could just set the "value" parameter of the form input to the current value of the setting:

<input type="text" value="<?php echo htmlentities($settings['title']); ?>" />

02-04-2008, 08:39 PM
Thank you very much!!! :thumbsup:
I'm thinking to to build a CMS but I feel like a noob. Sometimes I don't know such basic functions! :D

Thanks again :)

02-04-2008, 09:25 PM
And one more question. It's slightly off topic, but still, it's related to this file: how to deal with unicode characters in that inputs? When I type some unicode characters (Ex. აააა), it writes into the target file well, without any problem, but when it's viewed in input field, it shows up some nasty characters instead of normal symbols (Like this: &#195;&#161;&#194;�&#194;�).
I saved both documents in UTF-8 format and I tried to user htmlscpecialchars, and utf8 functions, but no use :(


Fixed. I used html_entity_decode ;)