如何解决preg_match_all 使用 php preg_match_all 从 httpd-vhosts.conf 文件 xampp 获取虚拟主机列表
这是我尝试过的,我也不想要用#/ ## 注释的主机和解释
$str = '# Virtual Hosts
#
# required modules: mod_log_config
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn\'t need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option \'-S\' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
##NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ##ServerName or ##ServerAlias in any <VirtualHost> block.
#
##<VirtualHost *:80>
##ServerAdmin webmaster@dummy-host.example.com
##DocumentRoot "D:/xampp/htdocs/dummy-host.example.com"
##ServerName dummy-host.example.com
##ServerAlias www.dummy-host.example.com
##ErrorLog "logs/dummy-host.example.com-error.log"
##CustomLog "logs/dummy-host.example.com-access.log" common
##</VirtualHost>
##<VirtualHost *:80>
##ServerAdmin webmaster@dummy-host2.example.com
##DocumentRoot "D:/xampp/htdocs/dummy-host2.example.com"
##ServerName dummy-host2.example.com
##ErrorLog "logs/dummy-host2.example.com-error.log"
##CustomLog "logs/dummy-host2.example.com-access.log" common
##</VirtualHost>
##<VirtualHost *:8080>
##ServerName CI1
##DocumentRoot D:\xampp\htdocs\codeigniter_1\public
##</VirtualHost>
<VirtualHost *:8080>
ServerName CI2
DocumentRoot D:\xampp\htdocs\codeigniter_2\public
</VirtualHost>
<VirtualHost *:8080>
ServerName CI3
DocumentRoot D:\xampp\htdocs\codeigniter_3\public
</VirtualHost>';
模式 1
$pattern1 = "#<\s*?$tagname\b[^>]*>(.*?)</$tagname\b[^>]*>#s";
preg_match_all($pattern1,$str,$match);
模式 2
$pattern2 = "/^(?<!#).*<$tagname.*>(.+?)<\/tagname>/mis";
preg_match_all($pattern2,$matches);
功能
function everything_in_tags($str,$tagname)
{
$pattern1 = "#<\s*?$tagname\b[^>]*>(.*?)</$tagname\b[^>]*>#s";
preg_match_all($pattern1,$match);
$pattern2 = "/^(?<!#).*<$tagname.*>(.+?)<\/$tagname>/s";
preg_match_all($pattern2,$matches);
echo '<pre>',print_r($match[1]),'</pre>';
echo '<pre>',print_r($matches[1]),'</pre>';
}
everything_in_tags($str,$tagname);
模式 1 的输出
Array
(
[0] => block.
#
##
##ServerAdmin webmaster@dummy-host.example.com
##DocumentRoot "D:/xampp/htdocs/dummy-host.example.com"
##ServerName dummy-host.example.com
##ServerAlias www.dummy-host.example.com
##ErrorLog "logs/dummy-host.example.com-error.log"
##CustomLog "logs/dummy-host.example.com-access.log" common
##
[1] =>
##ServerAdmin webmaster@dummy-host2.example.com
##DocumentRoot "D:/xampp/htdocs/dummy-host2.example.com"
##ServerName dummy-host2.example.com
##ErrorLog "logs/dummy-host2.example.com-error.log"
##CustomLog "logs/dummy-host2.example.com-access.log" common
##
[2] =>
##ServerName CI1
##DocumentRoot D:\xampp\htdocs\codeigniter_1\public
##
[3] =>
ServerName CI2
DocumentRoot D:\xampp\htdocs\codeigniter_2\public
[4] =>
ServerName CI3
DocumentRoot D:\xampp\htdocs\codeigniter_3\public
)
模式 2 的输出
Array
(
[0] =>
ServerName CI3
DocumentRoot D:\xampp\htdocs\codeigniter_3\public
)
期望输出
Array
(
[0] => array(
[ServerName] : CI2
[DocumentRoot] : D:\xampp\htdocs\codeigniter_2\public
),[1] => array(
[ServerName] : CI3
[DocumentRoot] : D:\xampp\htdocs\codeigniter_3\public
);
)
任何帮助将不胜感激,因为我是正则表达式的新手......而且我不需要任何用#/## 注释的字符串,提前致谢..
解决方法
用作正则表达式:
'~^\s*<VirtualHost\s+[^>]*>\s*ServerName\s+(?P<ServerName>\S+)\s*DocumentRoot\s+(?P<DocumentRoot>\S+)\s*</VirtualHost>~m'
那么您需要的是捕获组 1/'ServerName' 和 2/'DocumentRoot'。
说明:
-
^
- 匹配带有m
标志集的行首。 -
\s*
- 匹配 0 个或多个空白字符。 -
<VirtualHost
- 匹配“ ”。\s+
- 匹配 1 个或多个空白字符。[^>]*
匹配 0 个或多个不是“>”的字符。>
- 匹配“>”。\s*
- 匹配 0 个或多个空白字符。ServerName
- 匹配“服务器名称”。\s+
- 匹配 1 个或多个空白字符,(?P<ServerName>\S+)
- 匹配 1 个或多个非空白字符作为捕获组“ServerName”。\s*
- 匹配 0 个或多个空白字符。DocumentRoot
- 匹配“DocumentRoot”。\s+
- 匹配 1 个或多个空白字符,(?P<DocumentRoot>\S+)
- 匹配 1 个或多个非空白字符作为捕获组“DocumentRoot”。\s*
- 匹配 0 个或多个空白字符。</VirtualHost>
- 匹配“
代码:
$regex = '^\s*<VirtualHost\s+[^>]*>\s*ServerName\s+(?P<ServerName>\S+)\s*DocumentRoot\s+(?P<DocumentRoot>\S+)\s*</VirtualHost>~m';
preg_match_all($regex,$str,$matches,PREG_SET_ORDER);
print_r($matches);
打印:
Array
(
[0] => Array
(
[0] =>
<VirtualHost *:8080>
ServerName CI2
DocumentRoot D:\xampp\htdocs\codeigniter_2\public
</VirtualHost>
[ServerName] => CI2
[1] => CI2
[DocumentRoot] => D:\xampp\htdocs\codeigniter_2\public
[2] => D:\xampp\htdocs\codeigniter_2\public
)
[1] => Array
(
[0] =>
<VirtualHost *:8080>
ServerName CI3
DocumentRoot D:\xampp\htdocs\codeigniter_3\public
</VirtualHost>
[ServerName] => CI3
[1] => CI3
[DocumentRoot] => D:\xampp\htdocs\codeigniter_3\public
[2] => D:\xampp\htdocs\codeigniter_3\public
)
)
更新
上述正则表达式假定 DocumentRoot 规范不包含嵌入空格,但情况并非总是如此。它还有一个无关的、不必要的 s*
,我已将其删除。我相信以下正则表达式是一种改进。它将匹配 1 个或多个非空白字符的 \S+
替换为 .+?
,这将非贪婪匹配 1 个或多个 any 字符(换行符除外)。由于它后面是 \s*</VirtualHost>
,因此 non-greedy 它会在发现 ' 之前有任意空格时立即停止匹配。
所以新的正则表达式将是(与 m
标志一起使用):
^\s*<VirtualHost\s+[^>]*>\s*ServerName\s+(?P<ServerName>\S+)\s*DocumentRoot\s+(?P<DocumentRoot>.+?)\s*</VirtualHost>
$regex = '^\s*<VirtualHost\s+[^>]*>\s*ServerName\s+(?P<ServerName>\S+)\s*DocumentRoot\s+(?P<DocumentRoot>.+?)\s*</VirtualHost>~m';
preg_match_all($regex,PREG_SET_ORDER);
print_r($matches);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。