preg_match_all 使用 php preg_match_all 从 httpd-vhosts.conf 文件 xampp 获取虚拟主机列表
preg_match_all to get list of virtual hosts from httpd-vhosts.conf file xampp using php preg_match_all
这是我试过的,我也不想要用#/##评论的主机和解释
$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, $str, $matches);
函数
function everything_in_tags($str, $tagname)
{
$pattern1 = "#<\s*?$tagname\b[^>]*>(.*?)</$tagname\b[^>]*>#s";
preg_match_all($pattern1, $str, $match);
$pattern2 = "/^(?<!#).*<$tagname.*>(.+?)<\/$tagname>/s";
preg_match_all($pattern2, $str, $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
- 匹配 '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>
,是 非贪婪的 ,一旦它发现 ' 可选地前面有任何空格,它就会停止匹配。
所以新的正则表达式将是(与 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, $str, $matches, PREG_SET_ORDER);
print_r($matches);
这是我试过的,我也不想要用#/##评论的主机和解释
$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, $str, $matches);
函数
function everything_in_tags($str, $tagname)
{
$pattern1 = "#<\s*?$tagname\b[^>]*>(.*?)</$tagname\b[^>]*>#s";
preg_match_all($pattern1, $str, $match);
$pattern2 = "/^(?<!#).*<$tagname.*>(.+?)<\/$tagname>/s";
preg_match_all($pattern2, $str, $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
- 匹配 '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>
,是 非贪婪的 ,一旦它发现 ' 可选地前面有任何空格,它就会停止匹配。
所以新的正则表达式将是(与 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, $str, $matches, PREG_SET_ORDER);
print_r($matches);