-1

I have a string like this:

..., "test1@test1.com" <test1@test1.com>, "test2@test2.com" <test2@test2.com>, "test3@test3.com", "test4@test4.com" <test4@test4.com>, ....

I am exploding everything by , , but problem is that i dont want to have value laike this [0] => "test1@test1.com" <test1@test1.com> i need to remove the emails which are in those <..> brackets.

So the result should be like this [0] => test1@test1.com. Any offers how to drop the second phrase?

hakre
  • 193,403
  • 52
  • 435
  • 836
Gntvls
  • 230
  • 4
  • 16
  • 1
    Please look for existing questions. This or something very similar should have been asked and answered already. You might be looking for: http://php.net/imap_rfc822_parse_adrlist – hakre Sep 27 '13 at 08:02
  • Be aware that the part inside the quotation mark is free-text, it may be the name of the recipient or something else. To get an email address you should use the parts within the brackets, not the part within the quotation marks. (e.g. `"Peter Peng" `) – feeela Sep 27 '13 at 08:13

8 Answers8

4

You can make use of a function that has been especially tailored for such email address lists, for example imap_rfc822_parse_adrlist. Mapping it and extracting the information you need might do it already:

$list     = ""test1@test1.com" <test1@test1.com>, "test2@test2.com" <test2@test2.com>, "test3@test3.com", "test4@test4.com" <test4@test4.com>";
$adresses = array_map(function($entry) {
    return sprintf('%s@%s', $val->mailbox, $val->host);
}, imap_rfc822_parse_adrlist($list, ""));

This has the benefit that it properly deals with the quoted printable text in front that you have - which done properly is non-trivial (really).

hakre
  • 193,403
  • 52
  • 435
  • 836
2

The simplest way here - use strip_tags function (see strip_tags description)

Ilya
  • 4,583
  • 4
  • 26
  • 51
  • Why the upvotes? This answer is *plain wrong*. I wonder why nobody sees? Blended by the light? @Ilya: Yes, please, see the strip_tags description your own. Read it. Compare with the question. Quick shot in the wrong direction I'd say, worth some DVs honestly. :) – hakre Sep 27 '13 at 08:12
1

Use Regular Expressions to replace anything between <...> for empty strings, then explode your modified string into an array.

Bardo
  • 2,470
  • 2
  • 24
  • 42
1
<?php
$str = '"test1@test1.com" <test1@test1.com>';
$str= preg_replace("(<.*>+)", "", $str);
print $str;
?>
Mayank Sharma
  • 844
  • 7
  • 21
  • 1
    Actually according to your question this is not what you want. Care to elaborate? – hakre Sep 27 '13 at 08:17
  • 1
    And if it was, please accept the answer that answered exactly so earlier than this one: http://stackoverflow.com/a/19045743/367456 - that is normally considered more fair. – hakre Sep 27 '13 at 08:22
1

The easiest way is to use preg_match:

preg_match('(<.*>+)', $your_emails, $matches);

print_r($matches); // array of zero or more matches depending on input
hakre
  • 193,403
  • 52
  • 435
  • 836
2hamed
  • 8,719
  • 13
  • 69
  • 112
1

You can explode your text into an array and the run a array_map with a function that cleans your text. Something like this:

function clean($t){
    //Use regexp to replace desired text
    return preg_replace('/<[^>]*>/', '', $t);
}    

$text = '"test1@test1.com" <test1@test1.com>, "test2@test2.com" <test2@test2.com>, "test3@test3.com", "test4@test4.com" <test4@test4.com>';
$a = explode(',', $text);
var_dump($a);


$b = array_map("clean", $a);
var_dump($b);
Ander2
  • 5,569
  • 2
  • 23
  • 42
0

It's a line of code:

array_map(function($a){ return trim($a, ' "'); }, explode(',', strip_tags($string)));

And the whole:

<?php
$string = <<<TK
"test1@test1.com" <test1@test1.com>, "test2@test2.com" <test2@test2.com>, "test3@test3.com", "test4@test4.com" <test4@test4.com>
TK;
$result = array_map(
            function($a){
                return trim($a, ' "');
            },
            explode(',', strip_tags($string))
    );
var_dump($result);

Output:

array(4) {
  [0]=>
  string(15) "test1@test1.com"
  [1]=>
  string(15) "test2@test2.com"
  [2]=>
  string(15) "test3@test3.com"
  [3]=>
  string(15) "test4@test4.com"
}
revo
  • 47,783
  • 14
  • 74
  • 117
  • According to the question, it's looking for the email addresses without the quotes around. In your output there are quotes around. – hakre Sep 27 '13 at 08:24
  • No problems with a quotes tags i did this for removing it : `$str = str_replace('"', "", $str);` – Gntvls Sep 27 '13 at 08:51
  • @hakre `trim` is specially made for this kind of use as its name clarifies its job, simpler and more readable. so if I was going to use `sscanf`, I preferred `preg_replace` more. – revo Sep 27 '13 at 08:58
  • trim has less control on which characters to match. Also you don't want to replace / filter but just to extract as the information exists verbatim, you only need to locate it. I would do filtering only if the first is not possible due to unlcear constraints which are not the case here. `preg_match` / `preg_replace` ship with more options but these options are not needed here, simpler regular expressions like those of `sscanf` already make it. These are merely the reasons why I suggested it. – hakre Sep 27 '13 at 09:01
  • @hakre what do you mean by saying `trim` has less control! It's just about removing `quotations` and `spaces`. also we don't want to extract! but we want to manipulate already extracted data and what `sscanf` actually does is filtering like preg_replace, how ever more strict and dirtier. – revo Sep 27 '13 at 09:35
  • @revo: For example: How many spaces and quotes? You can't say (and you normally don't want to when using trim (just those *around*). However if you know how many you actually want to remove (e.g. one quote before and a quote plus one space after) you can not control that exactly, e.g. it would remove any quotes and spaces around, regardless in which order and in which count. Trim does not allow you to control that behavior precisely. More clear? – hakre Sep 27 '13 at 09:38
  • And if you're looking for a very fine method, check preg_match and preg_split but you need to be able to write regular expressions well. Then you can do that in one call (if you're willing to ignore [RFC822](http://tools.ietf.org/html/rfc822)). – hakre Sep 27 '13 at 09:39
  • @hakre your last but one comment is definitely correct but coding will differ from case to case. Saying that, at OP case there is no need for more complexity than trimming. Neither preg_ functions nor sscanf. But different solutions, as always, are welcome. – revo Sep 27 '13 at 09:50
  • Yes sure, it was meant to discuss things not that there is only the one right way. Just wanted to tell you why I suggested that so it's more clear to you to make your own decisions not that you should decide the same way. – hakre Sep 27 '13 at 09:53
-1

if

$yourString='"test1@test1.com" <test1@test1.com>';

you can use:

$yourString=substr($yourString,1,strpos($yourString,'<')-3);

(edited)

Thanos Darkadakis
  • 1,669
  • 2
  • 18
  • 30