Franquis
Franquis

Reputation: 743

Storing a config file with variable in a database

I have several config files for routers. Variables are between < >

configure
   port aps-<x>
      description "<VAR1>;<port_APS>"
      aps
         neighbor <@IP_loopback3_NM_PAIRE>
         no revert-time
         working-circuit <x/y/z>  
      exit
      sonet-sdh
         path
            mtu 4494
            atm
            exit
            no shutdown
         exit
      exit
      no shutdown
   exit
exit

I wonder how i can store raw text into my database and then use php for building the final config with some values instead of my variables.... Just like a:

sprintf('port aps-%d',$aps_port);

but for the entirely text...

Any ideas?

Thks all!

Upvotes: 1

Views: 132

Answers (1)

netcoder
netcoder

Reputation: 67695

The easiest way is to use str_replace:

$vars = array(
   'x' => 'foo',
   'VAR1' => 'bar',
   'port_APS' => 'baz',
   '@IP_loopback3_NM_PAIRE' => 'hello',
   'x/y/z' => 'world',
);

$invars = array();
foreach (array_keys($vars) as $var) {
    $invars[] = "<$var>";
}

$text = str_replace($invars, $vars, $text);

Yields:

configure
   port aps-foo
      description "bar;baz"
      aps
         neighbor hello
         no revert-time
         working-circuit world  
      exit
      sonet-sdh
         path
            mtu 4494
            atm
            exit
            no shutdown
         exit
      exit
      no shutdown
   exit
exit

Upvotes: 2

Related Questions