正则表达式找到未注释的apache配置标签?

时间:2012-07-13 19:00:26

标签: php regex

首先看到这个用于apache virtualHost配置文件的注释标记:

##<VirtualHost *:80>
    ##ServerAdmin postmaster@dummy-host2.localhost
    ##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>

如何在主题之间找到未注释的标签并获取内容?

2 个答案:

答案 0 :(得分:2)

将此正则表达式^\s*[^#\s].+?$与正则表达式选项multyline一起使用,可以捕获matchCollection未注释的行

答案 1 :(得分:2)

$subject = '##<VirtualHost *:80>
##ServerAdmin postmaster@dummy-host2.localhost
##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>';

在$ matches [1]中,你会找到你想要的东西