Preg_match_all在动态标签之间


preg_match_all between dynamic tags

我想抓住我的每个虚拟主机配置,并使用preg_match_all将它们放在一个数组中,这样我就可以从每个主机中提取信息,例如…

$vHostConfig = '    <VirtualHost *:80>
        ServerName localhost
        DocumentRoot c:/wamp/www
        <Directory  "c:/wamp/www/">
           Options +Indexes +Includes +FollowSymLinks +MultiViews
           AllowOverride All
           Require local
        </Directory>
    </VirtualHost>
    <VirtualHost *:8080>
        ServerName testing.com
        DocumentRoot c:/wamp/www/testing.com
        <Directory  "c:/wamp/www/testing.com">
           Options +Indexes +Includes +FollowSymLinks +MultiViews
           AllowOverride All
           Require local
        </Directory>
    </VirtualHost>
    <VirtualHost 127.0.0.1:80>
        ServerName testing2.com
        DocumentRoot c:/wamp/www/testing2.com
        <Directory  "c:/wamp/www/testing2.com">
           Options +Indexes +Includes +FollowSymLinks +MultiViews
           AllowOverride All
           Require local
        </Directory>
    </VirtualHost>
#    <VirtualHost *:80>
#        ServerName testing3.com
#        DocumentRoot c:/wamp/www/testing3.com
#        <Directory  "c:/wamp/www/testing3.com">
#            Options +Indexes +Includes +FollowSymLinks +MultiViews
#            AllowOverride All
#            Require local
#        </Directory>
#    </VirtualHost>';
preg_match_all(<<what to put here>>, $vHostConfig, $vHostConfigMatches);

我想只抓取活动配置,在行开头没有#,这意味着我应该有三个字符串,从<VirtualHost开始,在$vHostConfigMatches数组中以</VirtualHost>结束。这可能吗?

您可以使用以下正则表达式:

preg_match_all('/^'h*<VirtualHost.*?>.*?'R'h*<'/VirtualHost>/sm',
               $vHostConfig, $vHostConfigMatches);  

注意,数组$vHostConfigMatches将有一个额外的嵌套级别,所以只取reset的第一个嵌套级别:

print_r(reset($vHostConfigMatches));

可以按行分割:$lines = explode(PHP_EOL, $vhostConfig);

过滤掉所有注释行:$lines = array_filter($lines, function ($ele) { return substring($ele, 0) != "#"; });

把它放回一起:$vhostConfig = implode(PHP_EOL, $lines);

然后使用正则表达式拉出每个虚拟主机(你可能想要更精确的东西:preg_match_all("@<VirtualHost ['d'.'*:]+>(.*?)</VirtualHost>@", $vhostConfig, $vhostConfigMatches);

未经测试,但应该给你一个想法。这样做还有一个好处,就是可以忽略有效虚拟主机

中的任何注释行

虽然@trincot的答案工作得很好,但它使用了.*?(惰性)量词,这使得regex引擎非常活跃:这个regex101显示它在这个例子中需要950步。

所以我认为,即使看起来有点复杂,这个简单的PHP代码段会运行得更快:

$result = array_reduce(
  explode(PHP_EOL, $str),
  function($result, $line) {
    if (trim($line[0]) <> '#') {
      if (strpos($line, '<VirtualHost') !== false) {
        $result[] = $line;
      } else {
        $result[count($result) - 1] .= $line;
      }
    }
    return $result;
  },
  []
);

立刻,它只是:

  • 将原始字符串转换为行数组
  • 删除任何注释
  • 按预期填充所需的结果