You are not logged in.
I am making a package and would like php to execute sed - command during setup (when user goes to installation php page and submits info). Like this:
exec("sed -i 's|^.*$db_host.*$|$db_host="$HOST";|' config.php");
exec("sed -i 's|^.*$db_user.*$|$db_user="$IPUSER";|' config.php");
exec("sed -i 's|^.*$db_password.*$|$db_password="$IPUSERPASS";|' config.php");
exec("sed -i 's|^.*$db_name.*$|$db_name="$DBNAME";|' config.php");
exec("sed -i 's|^.*$url_base.*$|$url_base="$URLBASE";|' config.php");
Apparently something is wrong with escapes because sed never gets any matches that it would replace.
Offline
Why spawn an external command? This does not work in safe-mode.
Better use php's builtin preg_replace.
Offline
ok, I could load that config.php into a variable, use that preg and then write changed stuff back to config.php. I'll try that now. Thanks!
Offline
I still have problems - regular expressions don't seem to work right - I think I am missing something. I have the config file where are these kinds of lines:
$db_host="localhost";
$db_user="iptables_user";
each setting on its own line. I would like the code to find those lines and change them. So I came up with this code:
$file = file_get_contents('config.php');
$file = preg_replace('/^.*$db_host.*$/',"$db_host="'$HOST'";",$file);
$file = preg_replace('/^.*$db_user.*$/',"$db_user="'$IPUSER'";",$file);
$file = preg_replace('/^.*$db_password.*$/',"$db_password="'$IPUSERPASS'";",$file);
when I finally try to echo $file, no changes are visible. I'll get to view just the original contents fo config.php.
Offline
I don't see why that wouldn't work... however, one thing you can do to make this a whole lot easier:
$match= array('/^.*$db_host.*$/',
'/^.*$db_user.*$/',
'/^.*$db_password.*$/);
$replace= array("$db_host="'$HOST'";",
"$db_user="'$IPUSER'";",
"$db_password="'$IPUSERPASS'";");
$file = preg_replace($match,$replace,file_get_contents('config.php'));
Offline
That's nicer But still the strings don't get replaced. I don't know if some php settings are causing this not to work.
Offline
$match= array('/^$db_host.*$/m',
'/^$db_user.*$/m',
'/^$db_password.*$/m');
$replace= array('$db_host="'.$HOST.'";',
'$db_user="'.$IPUSER.'";',
'$db_password="'.$IPUSERPASS.'";');
$file = preg_replace($match,$replace,file_get_contents('config.php'));
dont forget to write out the file too...
file_put_contents ('config.php', $file);
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
I tried to echo that $file directly and that did not show any change. So did not do that operation yet.. I'll release the package because I have set the default values in the set up form so that those who want can have it set up fast.
EDIT: I released it, its under name iptlogger
Offline
Arch Linux (Duke)
JabberID: cgill27@jabber.org
IRC: Aletheuo
Registered Linux User #354975
Offline
Yay!
Some PHP to look over! My favourite thing to do in the whole wide world!
I hope I'll have a solution for you some time soon.
Edit: Your problem is quite basic;
You have attached a ^ and $ at the beginning and end, respectively, of the search expression, meaning that you're basically looking for a file that contains nothing more than .*db_host.* . Your file contains a lot, more specifically, a lot of newlines, hence, the string isn't found.
You'll have a patch in a sec, to make it easier for you to fix it (cos I'm so damn nice).
Yet anther edit: Here you go!
I do realize that if you wrote a script of this size you have enough knowledge to fix this little mistake without the help of a patch, but I really felt like making one anyway. I don't know why.
Offline
did nobody notice that my code block in my last post actually works? lol..I even tested it.
and there is no reason to stick a .* on the front of the regexp. Just make sure the variables have no blank space at the start of the line in front of them.
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
and there is no reason to stick a .* on the front of the regexp. Just make sure the variables have no blank space at the start of the line in front of them.
Making sure you have no blank space before the variable is quite a demand for people obsessed with tabbing, like me.
I swear, if my tab key ever breaks, I'm gonna start smashing things.
I'm such a clean-code-whore.
Offline
cactus wrote:and there is no reason to stick a .* on the front of the regexp. Just make sure the variables have no blank space at the start of the line in front of them.
Making sure you have no blank space before the variable is quite a demand for people obsessed with tabbing, like me.
I swear, if my tab key ever breaks, I'm gonna start smashing things.
I'm such a clean-code-whore.
but this is a config file... do you really tab out your config files?
Offline
mmgm wrote:cactus wrote:and there is no reason to stick a .* on the front of the regexp. Just make sure the variables have no blank space at the start of the line in front of them.
Making sure you have no blank space before the variable is quite a demand for people obsessed with tabbing, like me.
I swear, if my tab key ever breaks, I'm gonna start smashing things.
I'm such a clean-code-whore.but this is a config file... do you really tab out your config files?
Config files are usually ment to be human-readable. This is why they usually contain comments and such. Tabbing makes code more readable and therefore I find it usefull in any file which contains text of any sort. In the end, it's going to make your life a whole lot easier.
Offline
#my config file
setting=value
setting_two=bill
setting_one=ted
vs
#my config file
setting=value
setting_two=bill
setting_one=ted
I see nothing making my life easier... actually, I now have to move my eyes from left to right after reading the comment, instead of just down... which makes my life just the opposite
Offline
#my config file setting=value setting_two=bill setting_one=ted
vs
#my config file setting=value setting_two=bill setting_one=ted
I see nothing making my life easier... actually, I now have to move my eyes from left to right after reading the comment, instead of just down... which makes my life just the opposite
Now try removing tabs from xorg.conf or fstab, for example.
Offline
Now try removing tabs from xorg.conf or fstab, for example.
fstab: I have no leading tabs/s (which is what we're talking about here... not between values)
xorg.conf: a sectioned config file, which is different, but again not what we're talking about...
If you read the original post (and, hell, the entire thread), you will see that we are talking about a php config file, of the form "$var=value". we are not talking about any other config files, i.e. xorg.conf, fstab, sshd_config, httpd.conf, etc etc
Offline
I'd still rather see:
<?php
$db["host"] = "localhost";
$db["name"] = "MY_DB";
$db["user"] = "mmgm";
?>
And not have all the variables squished under the <?php tag.
Also, I sense slight aggressiveness, so I must stress that I didn't mean to offend or disrespect anyone. Infact, I called myself a clean-code-whore, implying that my clean coding practices are excessive and not necessarily fit for anyone but a lunatic like myself.
I do however, believe that software should be made to work even in cases where the end user has strange convensions, or special needs (I know this doesn't apply in this case, but theoretically...)
Two more characters in a regex can't hurt that much, can they?
Offline
Also, I sense slight aggressiveness, so I must stress that I didn't mean to offend or disrespect anyone. Infact, I called myself a clean-code-whore, implying that my clean coding practices are excessive and not necessarily fit for anyone but a lunatic like myself.
Eh, maybe I'm cranky today - dunno... I'm also touchy about stuff like this - they seriously have code formatting training here (I opted out), and while everyone knows how to properly tab/space/indent their code, they're not worth crap when it comes to real coding - I had to tell someone, with a BS in Computer Science, mind you, how to join a table in sql... wtf? even if you'd never been exposed to it (she has), you could at least google it in 2 seconds...
Offline
revised to include spacing detection, and replacement with the same amount of prepended white space..
enjoy.
$match= array('/^([ ]*)$db_host.*$/m',
'/^([ ]*)$db_user.*$/m',
'/^([ ]*)$db_password.*$/m');
$replace= array('${1}$db_host="'.$HOST.'";',
'${1}$db_user="'.$IPUSER.'";',
'${1}$db_password="'.$IPUSERPASS.'";');
$file = preg_replace($match,$replace,file_get_contents('config.php'));
file_put_contents ('config.php', $file);
8)
Also, php config files should generally NOT be indented. There is no readability gain achieved from it. But, I guess it depends on the person doing the coding though.
Might I suggest you use the php_config parser if you need lots of config values written and/or read. [/code]
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
great Thanks, cactus and others! Those regexes have been the thing I wanted to learn better. You never know when you need them and then it can take looong until you figure out how to do the things right.
I've been using php more since this spring - I coded one project for almost two months and before only made some smaller exercises to try it out.
I was thinking about somekind of php framework for getting/changing configuration values,cactus. Please tell me what do you mean by using php_config parser.
Offline
I had to tell someone, with a BS in Computer Science, mind you, how to join a table in sql... wtf? even if you'd never been exposed to it (she has), you could at least google it in 2 seconds...
That's nothing. My CS teacher never saw variable assignment inside a conditional before I came along. God, people can be so stupid...
Offline
Please tell me what do you mean by using php_config parser.
http://us2.php.net/manual/en/function.p … i-file.php
I guess it only works for reading. I must have been thinking of some pear class that allows writing as well as reading...dunno
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
ahh yes..the aptly named one.
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline