Kirzilla
Kirzilla

Reputation: 16606

Change constant, var value in php file

I'm writing my own tiny deployment script and I need to change db login and password in db.php file when copying it to remote host. Is it possible to change variable/constant value without hand-made analyzing (preg_replace etc.) text of php file?

I know that PHP have tokenizer, but I have no clue how to use it.

Please help me with some samples.

Any help and suggestions are appreciated.

Thank you.

Upvotes: 0

Views: 870

Answers (2)

vicTROLLA
vicTROLLA

Reputation: 1529

You may find phing overkill but it is quite useful. http://phing.info/trac/

Upvotes: 0

Jon
Jon

Reputation: 437914

A simple str_replace would suffice, for example:

$config = file_get_contents('config.dist');
$config = str_replace(
              array('##USERNAME##', '##PASSWORD##'),
              array($realUsername, $realPassword),
              $config);
file_put_contents('config.php', $config);

where config.dist would look something like this:

<?php
$username = '##USERNAME##';
$password = '##PASSWORD##';
?>

Upvotes: 2

Related Questions