|
|
|
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)
?> |
|
| 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 | | | Massreplace Categories : Filesystem, Regexps, Strings, PHP | | | Check if a file exists on a remote FTP server with PHP Categories : PHP, FTP, Regexps | | | file class , uploade file , download file already uploaded on another website Categories : PHP, PHP Classes, Filesystem, Web Services | | | Introduction to Language Files Categories : PHP, Filesystem, Beginner Guides | | | A flat file counter Categories : PHP, Cookies, Filesystem, Beginner Guides | | | Avoiding or Detecting high bit characters in a string. Useful when you want to create a valid RSS feed Categories : PHP, Strings, Unicode, Regexps, Rich Site Summary (RSS) | | | Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem, Directories, Arrays, PHP | | | ereg -- Regular expression match Categories : PHP, PHP Functions, Regexps | | | Creating a Language File Categories : PHP, Beginner Guides, Filesystem | | | BBCode Formatting String Categories : PHP, HTML, Regexps, Arrays | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | I need a trim function/regexp that will trim all " " from the ends of a string. Categories : Regexps, PHP, Strings | | | Random Image Display Categories : PHP, Filesystem, Graphics, HTML and 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
| |
|
|
|