tags:

views:

365

answers:

3

My regex skill is... bad. I have this email body.

Hello World

[cid:[email protected]]

Hello World

[cid:[email protected]] [cid:[email protected]]

Hello World

And what I need is an array of the filenames.

preg_match_all("/\[cid:(.*)\@/", $string, $matches);

echo "<pre>";
    print_r($matches);
echo "</pre>";

And I get the first filename fine. But not the 2nd and 3rd.

[0] => image002.png
[1] => [email protected]] [cid:image002.png

How can I change this regex so it works for any embedded file in an email body?

A: 

Try:

preg_match_all("/\[cid:([^@]*)/", $string, $matches);

EDIT: Original post had a bug - apologies! I was trying to short circuit the greediness in an incorrect manner.

Stephen Doyle
Does not work sorry.
Ólafur Waage
Oops - my mistake. Corrected version is in the edit update. Works now.
Stephen Doyle
+4  A: 

I think you can just change the expression to this:

"/\[cid:(.*?)\@/"

To make the match non-greedy.

Here are a couple of tools you can use to test your expressions:

braveterry
Works, thanks .
Ólafur Waage
Better, because more explicit: "/\[cid:[^\]@]*/"
Tomalak
Tomalak - it should be [^@] instead of [^\] - there is no '\' in the file name.
Stephen Doyle
A: 

Please note that if you have the whole email (headers and all) you will get more consistent results by extracting the filenames from the email headers rather than the body (the format of which is heavily dependent on the chain of mail software that the email passed through before it reached you).

Read up on MIME, multipart messages and the MIME headers to see how to do this: http://en.wikipedia.org/wiki/Multipart_message#MIME_headers

jmtd
I have the email parser, what I want to do is create HTML with the a link to the physical image in the system.
Ólafur Waage