2012-07-13 42 views
1

第一眼看到这个评论标签是为Apache虚拟主机配置文件:正则表达式查找未注释的apache配置标记?

##<VirtualHost *:80> 
    ##ServerAdmin [email protected] 
    ##DocumentRoot "C:/xampp/htdocs/dummy-host2.localhost" 
    ##ServerName dummy-host2.localhost 
    ##ServerAlias www.dummy-host2.localhost 
    ##ErrorLog "logs/dummy-host2.localhost-error.log" 
    ##CustomLog "logs/dummy-host2.localhost-access.log" combined 
##</VirtualHost> 

如何才能找到取消注释标签并获得主题之间的内容?

+1

而不是正则表达式,你可能是最好找一个Apache配置文件的解析器。 [这可能会工作](http://pear.php.net/manual/en/package.configuration.config.avail-container.apache.php)。 – Wiseguy 2012-07-13 19:05:20

+0

@Wiseguy Realy tnx bor ...那不是我想要的...但它是非常好的 – 2012-07-13 19:07:51

回答

2

使用这个表达式^\s*[^#\s].+?$用正则表达式选项multyline你赶上matchCollection取消注释行

+0

它不工作:( – 2012-07-13 19:14:41

+0

应该是“多行”? – octern 2012-07-13 22:59:02

2
$subject = '##<VirtualHost *:80> 
##ServerAdmin [email protected] 
##DocumentRoot "C:/xampp/htdocs/dummy-host2.localhost" 
##ServerName dummy-host2.localhost 
##ServerAlias www.dummy-host2.localhost 
##ErrorLog "logs/dummy-host2.localhost-error.log" 
##CustomLog "logs/dummy-host2.localhost-access.log" combined 
##</VirtualHost>'; 

$pattern = '=[#]{1,}<VirtualHost.[^>]*>(.*?)</VirtualHost>=is'; 
$result = preg_match_all($pattern, $subject, $matches); 

echo '<pre>'.print_r($matches, true).'</pre>'; 

在$匹配[1],你会发现你在找什么

+0

tnx但它没有字... – 2012-07-13 19:24:44

+0

当然它的作品: 在这里检查:http://www.pagecolumn.com/tool/pregtest.htm – Besnik 2012-07-13 19:29:22

+0

在这里演示和来源:http://besnik.de/test.php – Besnik 2012-07-13 19:39:44