tags:

views:

89

answers:

3

I have following string:

"Test, User" < [email protected] >, "Another, Test" < [email protected] >, .........

I want following result:

array(
  array('name' => 'Test, User', 'email' => '[email protected]'),
  array('name' => 'Another, Test', 'email' => '[email protected]'),  
  ...........
)
A: 

Why don't you preg_split by pattern matching the:

"Test, User" < test[at]test.com >,

Then preg_match to find the name and email components, then put them in an array.

jjclarkson
+5  A: 

preg_match_all() seems appropriate:

$in = '"Test, User" < [email protected] >, "Another, Test" < [email protected] >, .........';
preg_match_all('!"(.*?)"\s+<\s*(.*?)\s*>!', $in, $matches);
$out = array();
for ($i=0; $i<count($matches[0]); $i++) {
  $out[] = array(
    'name' => $matches[1][$i],
    'email' => $matches[2][$i],
  );
}
print_r($out);

Output:

Array
(
    [0] => Array
        (
            [name] => Test, User
            [email] => [email protected]
        )

    [1] => Array
        (
            [name] => Another, Test
            [email] => [email protected]
        )

)
cletus
Would it be better if you use [^<>] instead of the . in the name. Then you can let the dot(.) be greedy? You could do for both the dots too.
Jass
What does "!" in regex mean? Just wondering.
serg
Ofcourse whatever works is fine! Just asking .. ;)
Jass
@Jass: Are there any advantages of dot being greedy?
serg
@serg555 ! is not used as a part of the regex here. Its a tokenizer in php for the regex !...! defines start and end of the regex
Jass
serg555: "!" has no special meaning in regex. In this example it's used as a token character in place of the more common "/" as allowed by Perl compatible regular expressions.
Kaivosukeltaja
Often non-greedy wildcards provide a cleaner solution than, say, [^"] but usually those can work too.
cletus
@serg555 i meant token not tokenizer.. And about the dot being greedy. I just think it would be slightly faster than a lazy one. but in case of failure it would backtrack and slow down in case of no match. [you could make it an atomic regex to fail fast and avoid useless backtracking]
Jass
@cletus this solution is working thanks a lot..
Kurund Jalmi
@cletus ah yeah i agree about the cleaner solution. ? is easier to read as compared to a negated character class for a beginner atleast.
Jass
A: 
$strs = preg_split($in,'".*" < .* >,');
foreach ($strs as $str){
$in1 = preg_match('/".+"/', $str);
$in2 = preg_match('/< .+ >/', $str);
push($out,array('name'=>$in1,'email'=>$in2);
}
echo $out;
CodeJoust
Doesn't work. There are commas in the name.
cletus
right.. can't use commas to split...
Kurund Jalmi