【问题标题】:preg_match_all to get list of virtual hosts from httpd-vhosts.conf file xampp using php preg_match_allpreg_match_all 使用 php preg_match_all 从 httpd-vhosts.conf 文件 xampp 获取虚拟主机列表
【发布时间】:2021-07-30 23:06:50
【问题描述】:

这是我尝试过的,我也不想要用#/ ##评论的主持人和解释

$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
        );
)

任何帮助将不胜感激,因为我是正则表达式的新手.....另外,我不需要任何用 #/## 注释的字符串,在此先感谢..

【问题讨论】:

    标签: php regex xampp


    【解决方案1】:

    用作正则表达式:

    '~^\s*<VirtualHost\s+[^>]*>\s*ServerName\s+(?P<ServerName>\S+)\s*DocumentRoot\s+(?P<DocumentRoot>\S+)\s*</VirtualHost>~m'
    

    那么您需要的是捕获组 1/'ServerName' 和 2/'DocumentRoot'。

    说明:

    1. ^ - 匹配行首与 m 标志集。
    2. \s* - 匹配 0 个或多个空白字符。
    3. &lt;VirtualHost - 匹配“
    4. \s+ - 匹配 1 个或多个空白字符。
    5. [^&gt;]* 匹配 0 个或多个不是 '>' 的字符。
    6. &gt; - 匹配“>”。
    7. \s* - 匹配 0 个或更多空白字符。
    8. ServerName - 匹配“服务器名称”。
    9. \s+ - 匹配 1 个或多个空白字符,
    10. (?P&lt;ServerName&gt;\S+) - 匹配 1 个或多个非空白字符作为捕获组“ServerName”。
    11. \s* - 匹配 0 个或更多空白字符。
    12. DocumentRoot - 匹配“DocumentRoot”。
    13. \s+ - 匹配 1 个或多个空白字符,
    14. (?P&lt;DocumentRoot&gt;\S+) - 匹配 1 个或多个非空白字符作为捕获组“DocumentRoot”。
    15. \s* - 匹配 0 个或更多空白字符。
    16. &lt;/VirtualHost&gt; - 匹配“
    17. ”。

    See RegEx Demo

    代码:

    $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*,我已将其删除。我相信以下正则表达式是一种改进。它用.+? 替换\S+,后者匹配1 个或多个非空白字符,.+?非贪婪地 匹配1 个或多个any 字符(除了换行符)。因为它后面跟着\s*&lt;/VirtualHost&gt;,它是非贪婪的,一旦发现' 可选地前面有任何空格,它就会停止匹配。

    所以新的正则表达式将是(与m 标志一起使用):

    ^\s*<VirtualHost\s+[^>]*>\s*ServerName\s+(?P<ServerName>\S+)\s*DocumentRoot\s+(?P<DocumentRoot>.+?)\s*</VirtualHost>
    

    See new RegEx Demo

    $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);
    

    【讨论】:

    • 非常感谢您的帮助,您确实节省了我的时间,我花了 2 天多的时间在 google 中搜索此内容,但没有任何答案对我有帮助。再次抱歉,我不能投票,因为我没有 15 的声誉。但我这边的无限投票。谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多