【发布时间】: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
);
)
任何帮助将不胜感激,因为我是正则表达式的新手.....另外,我不需要任何用 #/## 注释的字符串,在此先感谢..
【问题讨论】: