|
|
As I found thousands of sample how to use ereg, replace and file functions, but no proper
answer on how to edit a config-file (I became too so lazy to manually edit all this stuff :)), I
wrote some code myself and the first part is here.
Usage: http://confedit.php?
strCfgFile=YOUR_CONFIG_FILE&strCfgVar=THE_VAR_TO_MODIFY&strCfgVal=THE_NEW_VALUE
Here's the content for confedit.php:
------ CUT ----------------
<?
$strOldContent = file ($strCfgFile);
$strNewContent = "";
while (list ($intLineNum, $strLine) = each ($strOldContent))
{
if(!eregi("^//",$strLine)) // dont show any line commented with //
{
if(eregi("^\\$".$strCfgVar."( |\t)*=",$strLine)) // show any line beginning with a $
{
$strLineParts=explode("=",$strLine);
// here we should have to determine type of value! (BOOL, INT or String)
if("$".$strCfgVar == trim($strLineParts[0]))
{
$strLineParts[1] = "\t\"".$strCfgVal."\"";
$strLine = implode("=",$strLineParts).";\r\n";
}
}
}
$strNewContent .= $strLine;
$fp = fopen($strCfgFile."_new", "w");
fputs($fp,$strNewContent);
fclose($fp);
}
copy($strCfgFile."_new",$strCfgFile);
echo "<pre>The value for <b>$$strCfgVar</b> has been replaced with <b>$strCfgVal</b> in
File <b>$strCfgFile</b></pre>";
?>
----- End CUT ----
As I did not have the time yet to explore all power of regular expressions, I dont mind, a Guru
finding a more elegant solution for the first two ereg-statement to merge them.
Otherwise, this little script will end up in a complete app, with which one can (finally) simply
configure and setup all kind of php-apps, which require many times heavy editing.
The full version will include form-based editing. Any comments are very welcome, esp. hints, if
such script already exists (could save me more time ;)
Enjoy
n-dee (aka ARK)
?> |
|
| Massreplace Categories : Filesystem, Regexps, Strings, PHP | | | Show Source with Line Numbers Categories : PHP, Regexps, Filesystem | | | A simple configuration file editor to ease you life in setting up php applications. Reads variables from a given file automatically and displays current value. New value will be written to file after submit. Categories : PHP, Filesystem, Regexps, Java Script | | | List the content of the directory of your webserver where this small PHP Script resides. Categories : PHP, Filesystem, Directories, CSS | | | Search and Replace Text : Searches Files for Specified Text and Replaces It by a Given Text Categories : PHP, PHP Classes, Search, Filesystem | | | PHP Domain Availability Checker Categories : PHP, Complete Programs, Regexps, HTTP, Sockets | | | Bs_IniHandler is a class that can read and write ini-style files (and strings) Categories : PHP, Filesystem, PHP Classes | | | Functions to read a template file and fill in PHP variables. It will also fill in array variables, displaying parts of the template multiple times.
Categories : PHP, Variables, Filesystem | | | validateEmail 2.0 - upgraded version of the old validateEmail function used to validate email
addresses via SMTP and regex. Categories : Email, Regexps, PHP | | | Newbie Notes #7 - Ridiculous regex Categories : PHP, Beginner Guides, Regexps | | | PHP Transfer data from text file to Mysql Table Categories : PHP, PHP Classes, Filesystem, Databases, MySQL | | | filesystem Show Files Script Categories : PHP, Filesystem, Java Script | | | How to check if a file is of type gif or jpg? Categories : PHP, Regexps, Graphics | | | An efficient iterative and buffered text file reader Categories : PHP, Classes and Objects, Filesystem, PHP Classes, Log Files | | | how can I read the entire contents of a file into a string? Categories : Filesystem, Strings, PHP | |
| |
| | | | | Andy Krause wrote :481
stupid me -
the first eregi-statement is not necessary, if I just want to parse all lines starting with a "$".
n-dee
| |
|
|