<?php
$str = '"test1@test1.com" <test1@test1.com>';
$str= preg_replace("(<.*>+)", "", $str);
print $str;
?>
Remove phrases from string
-
29-06-2022 - |
Question
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?
Solution 3
OTHER TIPS
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).
The simplest way here - use strip_tags function (see strip_tags description)
Use Regular Expressions to replace anything between <...> for empty strings, then explode your modified string into an array.
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);
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
if
$yourString='"test1@test1.com" <test1@test1.com>';
you can use:
$yourString=substr($yourString,1,strpos($yourString,'<')-3);
(edited)
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"
}