简体   繁体   English

动态标签之间的preg_match_all

[英]preg_match_all between dynamic tags

I would like to grab each of my virtual hosts configurations and put them in an array using preg_match_all so I can extract information from each of them, for example... 我想抓住每个虚拟主机配置并使用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);

I would like to grab only the active configurations without a # at the beginning of the line meaning I should have three strings starting with <VirtualHost and finishing with </VirtualHost> in $vHostConfigMatches array. 我想在行的开头只抓取没有#的活动配置,这意味着我应该在$ vHostConfigMatches数组中有三个以<VirtualHost开头并用</VirtualHost>结束的字符串。 Is this possible? 这可能吗?

You could use this regular expression: 你可以使用这个正则表达式:

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

Note that the array $vHostConfigMatches will have an additional nesting level, so just take the first one with reset : 请注意,数组$vHostConfigMatches将具有额外的嵌套级别,因此只需使用reset的第一个:

print_r(reset($vHostConfigMatches));

You could split it by line: $lines = explode(PHP_EOL, $vhostConfig); 您可以按行拆分: $lines = explode(PHP_EOL, $vhostConfig);

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

Put it back together: $vhostConfig = implode(PHP_EOL, $lines); 把它放回去: $vhostConfig = implode(PHP_EOL, $lines);

Then use a regex to pull each virtual host (you may want something more precise: preg_match_all("@<VirtualHost [\\d\\.\\*:]+>(.*?)</VirtualHost>@", $vhostConfig, $vhostConfigMatches); 然后用正则表达式拉出每个虚拟主机(你可能想要更精确的东西: preg_match_all("@<VirtualHost [\\d\\.\\*:]+>(.*?)</VirtualHost>@", $vhostConfig, $vhostConfigMatches);

Untested, but should give you the idea. 未经测试,但应该给你的想法。 This also has the benefit of ignoring any commented line in a valid virtualhost 这也有利于忽略有效虚拟主机中的任何注释行

Although the @trincot's answer works fine, it uses the .*? 虽然@ trincot的答案运行正常但它使用的是.*? (lazy) quantifier which makes the regex engine highly active: this regex101 shows it takes 950 steps in this example. (懒惰)量词使正则表达式引擎高度活跃:此regex101显示在此示例中需要950步。

So I think that, even if it seems a bit more complicated, this simple PHP snippet would run faster: 所以我认为,即使它看起来有点复杂,这个简单的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;
  },
  []
);

At once, it merely: 马上,它只是:

  • turns the original string into an array of lines 将原始字符串转换为行数组
  • drop any comment 删除任何评论
  • populates the needed result as expected 按预期填充所需的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM