tags:

views:

65

answers:

2

Hello guys, I have such text:

<[email protected]> If you do so, please include this problem report. 
<[email protected]> You can delete your

own text from the attached returned message.

                   The mail system

<[email protected]>: connect to *.net[82.*.86.*]: Connection timed
out

I have to parse email from it. Could you help me with this job?


upd

There could be another email addresses in <%here%>. There should be connection between 'The mail system' text. I need in email which goes after that text.

+3  A: 

Considering this text is stored in $text, what about this :

$matches = array();
if (preg_match('/<([^>]+)>/', $text, $matches)) {
  var_dump($matches[1]);
}

Which gives me :

string '[email protected]' (length=13)


Basically, I used a pretty simple regex, that matches :

  • a < character
  • anything that's not a > character : [^>]
    • at least one time : [^>]+
    • capturing it : ([^>]+)
  • a > character

So, it captures anything that's between < and >.


Edit after comments+edit of the OP :

If you only want the e-mail address that's after The mail system, you could use this :

$matches = array();
if (preg_match('/The mail system\s*<([^>]+)>/', $text, $matches)) {
  var_dump($matches[1]);
}

In addition to what I posted before, this expects :

  • The string The mail system
  • Any number of white-characters : \s*
Pascal MARTIN
There could be another emails in brakes: <[email protected]>. There should be connect between, for example, text 'Mail system' and email
Ockonal
Hu... not quite sure what you want, then... Can you edit your question, to give us a couple of additionnal examples, with the expects outputs ?
Pascal MARTIN
Please, see updated post.
Ockonal
+2  A: 

You want to use preg_match() and looking at this input it should be simple:

<?php

if (preg_match('/<([^>]*?@[^>]*>/', $data, $matches)) {
  var_dump($matches); // specifically look at $matches[1]
}

There are other patterns that would match it, you don't have to stick to that same pattern. The '<' and '>' in your input are helpful here.

d11wtq