You are not logged in.

#1 2005-07-05 12:11:40

sven
Member
Registered: 2005-02-01
Posts: 311

help with php and regular expressions

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

#2 2005-07-05 13:13:04

juergen
Developer
From: Frankfurt/Germany
Registered: 2005-04-11
Posts: 48
Website

Re: help with php and regular expressions

Why spawn an external command? This does not work in safe-mode.
Better use php's builtin preg_replace.

Offline

#3 2005-07-05 13:24:31

sven
Member
Registered: 2005-02-01
Posts: 311

Re: help with php and regular expressions

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

#4 2005-07-05 15:09:47

sven
Member
Registered: 2005-02-01
Posts: 311

Re: help with php and regular expressions

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

#5 2005-07-05 15:46:42

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: help with php and regular expressions

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

#6 2005-07-06 06:50:51

sven
Member
Registered: 2005-02-01
Posts: 311

Re: help with php and regular expressions

That's nicer smile But still the strings don't get replaced. I don't know if some php settings are causing this not to work.

Offline

#7 2005-07-06 07:28:48

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: help with php and regular expressions

$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

#8 2005-07-06 10:04:11

sven
Member
Registered: 2005-02-01
Posts: 311

Re: help with php and regular expressions

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

#9 2005-07-06 14:03:55

Aletheuo
Member
Registered: 2004-03-26
Posts: 59

Re: help with php and regular expressions


Arch Linux (Duke)
JabberID:  cgill27@jabber.org
IRC: Aletheuo
Registered Linux User #354975

Offline

#10 2005-07-06 14:29:20

mmgm
Member
From: Israel
Registered: 2005-05-26
Posts: 52
Website

Re: help with php and regular expressions

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

#11 2005-07-06 15:48:32

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: help with php and regular expressions

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

#12 2005-07-06 15:55:16

mmgm
Member
From: Israel
Registered: 2005-05-26
Posts: 52
Website

Re: help with php and regular expressions

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.

Offline

#13 2005-07-06 17:35:41

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: help with php and regular expressions

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?

Offline

#14 2005-07-06 18:03:06

mmgm
Member
From: Israel
Registered: 2005-05-26
Posts: 52
Website

Re: help with php and regular expressions

phrakture wrote:
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

#15 2005-07-06 18:08:00

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: help with php and regular expressions

#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

#16 2005-07-06 18:10:55

mmgm
Member
From: Israel
Registered: 2005-05-26
Posts: 52
Website

Re: help with php and regular expressions

phrakture wrote:
#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.
tongue

Offline

#17 2005-07-06 18:19:29

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: help with php and regular expressions

mmgm wrote:

Now try removing tabs from xorg.conf or fstab, for example.
tongue

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

#18 2005-07-06 18:31:46

mmgm
Member
From: Israel
Registered: 2005-05-26
Posts: 52
Website

Re: help with php and regular expressions

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

#19 2005-07-06 18:58:52

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: help with php and regular expressions

mmgm wrote:

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

#20 2005-07-06 19:37:33

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: help with php and regular expressions

revised to include spacing detection, and replacement with the same amount of prepended white space..
enjoy. smile

$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

#21 2005-07-06 20:26:29

sven
Member
Registered: 2005-02-01
Posts: 311

Re: help with php and regular expressions

great big_smile 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

#22 2005-07-06 22:14:21

mmgm
Member
From: Israel
Registered: 2005-05-26
Posts: 52
Website

Re: help with php and regular expressions

phrakture wrote:

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

#23 2005-07-06 22:35:11

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: help with php and regular expressions

sven wrote:

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

#24 2005-07-06 22:41:27

mmgm
Member
From: Israel
Registered: 2005-05-26
Posts: 52
Website

Re: help with php and regular expressions

cactus wrote:

. I must have been thinking of some pear class that allows writing as well as reading...dunno

Proabaly this one.

Offline

#25 2005-07-07 00:17:18

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: help with php and regular expressions

ahh yes..the aptly named one. wink


"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

Board footer

Powered by FluxBB