Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中使用正则表达式读取文件并提取数据_Php_Regex - Fatal编程技术网

在PHP中使用正则表达式读取文件并提取数据

在PHP中使用正则表达式读取文件并提取数据,php,regex,Php,Regex,我正在尝试回显在logfile.txt中写入的文件的名称/路径。为此,我使用正则表达式匹配第一次出现:之前的所有内容并输出它。我正在逐行阅读logfile.txt: <?php $logfile = fopen("logfile.txt", "r"); if ($logfile) { while (($line = fgets($logfile)) !== false) { if (preg_match_all("/[^:]*/", $line, $matche

我正在尝试回显在
logfile.txt
中写入的文件的名称/路径。为此,我使用正则表达式匹配第一次出现
之前的所有内容并输出它。我正在逐行阅读
logfile.txt

<?php

$logfile = fopen("logfile.txt", "r");

if ($logfile) {
    while (($line = fgets($logfile)) !== false) {
        if (preg_match_all("/[^:]*/", $line, $matched)) {
            foreach ($matched as $val) {
                foreach ($val as $read) {
                    echo '<pre>'. $read . '</pre>';
                }
            }
        }
    }

    fclose($logfile);
} else {
    die("Unable to open file.");
}

?>
以下是
logfile.txt
的内容:

<?php
# load it as a string
$data = @file("logfile.txt");

# data for this specific purpose
$data = <<< DATA
-------------------------------------------------------------------------------

/home/user/public_html/an-ordinary-shell.php: Php.Trojan.PCT4-1 FOUND
/home/user/public_html/content/execution-after-redirect.html: {LDB}VT-malware33.UNOFFICIAL FOUND
/home/user/public_html/paypal-gateway.html: Html.Exploit.CVE.2015_6073
DATA;

$regex = '~^(/[^:]+):~m';
# ^ - anchor it to the beginning
# / - a slash
# ([^:]+) capture at least anything NOT a colon
# turn on multiline mode with m

preg_match_all($regex, $data, $files);
print_r($files);
?>
额外问题:如何跳过前两行(即破折号和emtpy行)?

给你:




它甚至会跳过两行,请参见

preg\u match\u all
返回模式的所有匹配项。对于第一行,它将返回:

/home/user/public\html/一个普通的shell.php

一个空字符串,
找到php.Trojan.PCT4-1
和另一个空字符串

不包含

要获得单个结果,请使用
preg_match
,但使用
explode
即可


例如,要跳过不需要的行,可以构建只给出好行的。您也可以使用流过滤器。

使用
preg\u match
而不是
preg\u match\u all
我本希望这个答案,但它输出两个数组。第一个包含
,第二个开始时文件名前没有斜杠。是否需要斜杠?更新答案,在
$files[1]
上循环以获取您的文件。另外还更新了ideone.com演示。假设我更新了。只是因为它们存在于文件中,而我最终可能需要它们来处理特定的案例。但我也希望只有一个数组。如果您可以确保每行开头都有一个斜杠的冒号(
),那么您也可以使用:
$regex='~^/[^:]+~m'(不再使用括号)-请记住,如果没有冒号(除冒号以外的任何内容也包括换行符),则它将匹配更多。括号的用途是什么?我接受了你的回答,谢谢。我真傻。我忘记了
preg\u match\u all
preg\u match
之间的区别。用
preg_match
完成并爆炸。谢谢你抽出时间。我也会接受你的回答,但我只能接受一个。
-------------------------------------------------------------------------------

/home/user/public_html/an-ordinary-shell.php: Php.Trojan.PCT4-1 FOUND
/home/user/public_html/content/execution-after-redirect.html: {LDB}VT-malware33.UNOFFICIAL FOUND
/home/user/public_html/paypal-gateway.html: Html.Exploit.CVE.2015_6073
<?php
# load it as a string
$data = @file("logfile.txt");

# data for this specific purpose
$data = <<< DATA
-------------------------------------------------------------------------------

/home/user/public_html/an-ordinary-shell.php: Php.Trojan.PCT4-1 FOUND
/home/user/public_html/content/execution-after-redirect.html: {LDB}VT-malware33.UNOFFICIAL FOUND
/home/user/public_html/paypal-gateway.html: Html.Exploit.CVE.2015_6073
DATA;

$regex = '~^(/[^:]+):~m';
# ^ - anchor it to the beginning
# / - a slash
# ([^:]+) capture at least anything NOT a colon
# turn on multiline mode with m

preg_match_all($regex, $data, $files);
print_r($files);
?>