views:

54

answers:

2

I have a text document that lists urls with their subject and an email address. I need to extract all urls with their subject and the email address and put this all into a csv file. I just need to know how I can use regex to do this. Currently I am able to extract all urls but I need the email and subject associated with them. This is what I am working with so far:

$file=file_get_contents('/data/urls.txt');
$pattern='([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%   [A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)';
preg_match_all($pattern, $file, $matches);

$matches=array_unique($matches[0]);

print_r($matches);

File structure:

Subject: URL

Email: [email protected]

Source URL: http://www.google.com

+1  A: 

How about this regular expression?

$pattern='/(Subject: (.*)\n\nEmail: (.*)\n\nSource URL: (.*))/';
antyrat
+1  A: 

Something like this may work for you, it depends how you are applying the term 'unique' to your input.

// reformat file
$pattern = '/Subject: (.*)[\n\r]+Email: (.*)[\n\r]+Source URL: (.*)[\n\r]*/';
$replace = '$1, $2, $3'."\n";
$output = preg_replace($pattern, $replace, $input);

// filter unique
$arr = explode("\n", $output);
$arr = array_unique($arr);

// output
$f = fopen('path.csv', 'w');
foreach($arr as $a) {
    fwrite($f, $a);
}
fclose($f);
Cags