2011-05-24 89 views
1

我有几个路由器配置文件。变量是<之间>在数据库中存储配置文件变量

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 

我不知道如何可以将原始文本存储到我的数据库,然后使用PHP的一些值,而不是我的变量构建最终的配置.... 就像一个:

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

但对于整个文本...

任何想法?

这一切!

回答

2

最简单的方法是使用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); 

产量:

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