opencombatclan Posted July 4, 2009 Share Posted July 4, 2009 Hello everyone. I am currently working on a little project. Now I tried to make a php config editor, but without success. I simply can not get it to work... This Is what i want to do. I have a file, named file.php <?php //Some //comments $flower = "red"; $case = "black"; $done = "0"; //end of config file ?> Now I need a function which does the following: 1.) Open the specific file (file.php) 2.) Search for the var $flower 3.) Change its value to "green" So the file will look like: <?php //Some //comments $flower = "green"; $case = "black"; $done = "0"; //end of config file ?> I tried it with fopen(), fread() and fclose(), but since the function does not know what is the value of $flower at the beginning, I did not get it to work. Can someone help me out? Link to comment https://forums.phpfreaks.com/topic/164739-solved-php-config-editor/ Share on other sites More sharing options...
ldougherty Posted July 4, 2009 Share Posted July 4, 2009 The function you are looking for would be SED which is a Search and Replace editor for Linux. I found a good article online which may assist you in using the functionality within PHP http://forums.tizag.com/showthread.php?t=9282 Hope that helps. Link to comment https://forums.phpfreaks.com/topic/164739-solved-php-config-editor/#findComment-868837 Share on other sites More sharing options...
opencombatclan Posted July 4, 2009 Author Share Posted July 4, 2009 Thks for the reply, but I still dont get it. How do i get this to work? Link to comment https://forums.phpfreaks.com/topic/164739-solved-php-config-editor/#findComment-868910 Share on other sites More sharing options...
newbtophp Posted July 4, 2009 Share Posted July 4, 2009 Are you looking for a form which will edit the config variables?. Like an installation file?. If yes use $_POST Link to comment https://forums.phpfreaks.com/topic/164739-solved-php-config-editor/#findComment-868913 Share on other sites More sharing options...
opencombatclan Posted July 5, 2009 Author Share Posted July 5, 2009 No I am looking for what i described in my first post Now I need a function which does the following: 1.) Open the specific file (file.php) 2.) Search for the var $flower 3.) Change its value to "green" (Please also see my examples for some further explanation) Link to comment https://forums.phpfreaks.com/topic/164739-solved-php-config-editor/#findComment-869191 Share on other sites More sharing options...
opencombatclan Posted July 5, 2009 Author Share Posted July 5, 2009 I fixed it using: <?php function set_var($file,$var,$val) { $array = file("$file",FILE_IGNORE_NEW_LINES); $lines = count($array); for($i=1; $i<$lines-1; $i=$i+1) { $line = $array[$i]; if ($line[0] == "$") { list($var_tmp, $val_tmp) = explode("=", $line); $variabele = substr($var_tmp, 1, -1); if ($var == $variabele) { $array[$i] = "$var_tmp= \"$val\";"; } } } $fh = fopen($file, 'w') or die("Could not update config.php"); $stringData = "<?php\n"; fwrite($fh, $stringData); $lines = count($array); for($i=1; $i<$lines-1; $i=$i+1) { $line = $array[$i]; fwrite($fh,"$line\n"); } $stringData = "?>"; fwrite($fh, $stringData); fclose($fh); } ?> This will do exactly what I asked in my first post. But its a huge pile of code, and I am sure it can be done way faster. Can anyone give some suggestions on how to optimize this code? Link to comment https://forums.phpfreaks.com/topic/164739-solved-php-config-editor/#findComment-869377 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.