1

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!

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.