[英]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);
我想在行的开头只抓取没有#的活动配置,这意味着我应该在$ vHostConfigMatches数组中有三个以<VirtualHost
开头并用</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的答案运行正常但它使用的是.*?
(懒惰)量词使正则表达式引擎高度活跃:此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;
},
[]
);
马上,它只是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.