2

I've wrote a relatively simple code to extract email addresses from a log file, based on a specific pattern; i'm only interested in cases when people sent emails to themselves.

This show in the log like this: <%EMAIL%> -> <%SAME-EMAIL%>

Clarification:

The <%EMAIL%> represents an email address inside "<>" characters, followed by " -> ", then followed by the same email address. This pattern may (or may not) occur multiple times in a line.

for example:

<[email protected]> -> <[email protected]> should match
<[email protected]> -> <[email protected]> should NOT match

the code i currently use:

$regx = '/(<[\S]+>)\s->\s\1/';
while ( !feof($myfile) )
{
    $line = fgets($myfile);
    $tmpline = $line;
    if ( preg_match_all($regx, $tmpline, $tmp) )
    {
        $data[$caught++] = $tmp;
    }
}
fclose($myfile);

My problem is, the $tmp array doesn't store the actual addresses only the " -> " substring. My output (print_r($data)) looks like this:

Array
(
    [0] => Array
    (
        [0] =>  -> 
    )

    [1] => Array
    (
        [0] => 
    )

)
...

I suspect, the problem lies in my regex pattern, but unfortunately I'm not yet capable of identifying it.

Please help.

Closing statement:

The output was correct all long, but the emails were inside <> characters, and my browser tried to parse them as HTML or XML, so they wouldn't appear when echoing them out :@

The solution was hiding in the view-source.

2 Answers 2

1

How about this?

$data = 
    preg_match_all('/<(\S+)>\s->\s<\1>/', file_get_contents($filename), $m)
    ? $m[1]
    : array()
;
print_r($data);

Demo on ideone.com


Another version using fgets().

$data = array();
$fp = fopen($filename, 'r');
while (false !== $row = fgets($fp)) {
    if (preg_match_all('/<(\S+)>\s->\s<\1>/', $row, $m)) {
        foreach ($m[1] as $email) {
            $data[] = $email;
        }
    }
}
if (!feof($fp)) {
    exit('Error');
}
print_r($data);

Demo on ideone.com

2
  • 1
    Seems nice, but the log files are extremely large (over 500k lines!), and as i recall, the file_get_contents would take too much resources. This is why I'm using a per line approach.
    – zedling
    Nov 30, 2014 at 10:16
  • 1
    Thank you. As it seems, my original solution was correct all long; I'm accepting yours, because it's a bit faster. The output was correct In your, and My case too, but the emails were inside <> characters, and my browser tried to parse them as HTML or XML, so they wouldn't appear when echoing them out :@ Thank you again.
    – zedling
    Nov 30, 2014 at 10:34
0

Good that you already have the solution. Not sure if this is an automated task or a one-time query. I usually use notepad++ (or a similar editor) for this kind of task which has excellent regexp replacement functionality, 500k lines should be no problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.