McJohnson McJohnson - 4 months ago 19
PHP Question

Reading a file and extracting data using a regex in PHP

I am trying to echo out the names/paths of the files that are written in

logfile.txt
. For that, I use a regex to match everything before the first ocurrence of
:
and output it. I am reading the
logfile.txt
line by line:

<?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.");
}

?>


However, I get the entire contents of the file instead. The desired output would be:

/home/user/public_html/an-ordinary-shell.php
/home/user/public_html/content/execution-after-redirect.html
/home/user/public_html/paypal-gateway.html


Here is the content of
logfile.txt
:

-------------------------------------------------------------------------------

/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


Extra question: How do I skip reading the first two lines (namely the dashes and emtpy line)?

Jan Jan
Answer

Here you go:

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


It even skips both your lines, see a demo on ideone.com.