santa
santa

Reputation: 12512

Import emails from a string

I would like to let my users paste a string of emails into a text are and on submit import them into a db table.

It seem that the typical syntax for email and they way it is usually typed in TO field is:

"Fname Lname" <[email protected]>, "Fname Lname" <[email protected]>,

I suppose on submit I need to parse it through some loop. How do I detect the name/email pair break? Is there a way to also do some basic email validation?


UPDATE:

I realized that sometimes the string can be slightly off, like missing a name:

Fname Lname" <[email protected]>, [email protected],

or even

<[email protected]>, [email protected],

or even

[email protected], [email protected],

How to make it more robust and note break if a name is missing? The email should be there all the time! Otherwise it just needs to ignore an instance and move on to the next contact. But I guess I can do that validation after the array of contacts is created...

Upvotes: 1

Views: 225

Answers (4)

Naveed
Naveed

Reputation: 42113

I am not good in preg_match, so I tried it in another way. If your string is always in this pattern, you can try something like this:

$str = '"Fname Lname" <[email protected]>, "Fname Lname" <[email protected]>';

$pairs = explode( ",", $str );

foreach( $pairs as $pair ) {

  $pair = explode( "<", $pair );
  $pair['0'] = trim( str_replace( "\"", "", $pair['0'] ) );
  $pair['1'] = trim( rtrim( $pair['1'], ">" ) );

  print_r( $pair ); // Save it to database here
}

Test Here

EDIT:

(Change in input string)

$str = '"Fname Lname" <[email protected]>, [email protected]';

$pairs = explode( ",", $str );

foreach( $pairs as $pair ) {

  if( strpos($pair, '<') ) {

    $output = explode( "<", $pair );
    $output['0'] = trim( str_replace( "\"", "", $output['0'] ) );
    $output['1'] = trim( rtrim( $output['1'], ">" ) );

  } else {

    $output = array();
    $output['0'] = '';
    $output['1'] = $pair;

  }

  print_r($output); // Save it to database here
}

Test Here

Upvotes: 2

Zuul
Zuul

Reputation: 16269

You can do it using preg_match_all() and for:

PHP Example

<?php

$string = '"Fname Lname" <[email protected]>, "Fname Lname" <[email protected]>,';
preg_match_all('!"(.*?)"\s+<\s*(.*?)\s*>!', $string, $matches);
$arr = array();
for ($i=0; $i<count($matches[0]); $i++) {
  $arr[] = array(
    'name' => $matches[1][$i],
    'email' => $matches[2][$i],
  );
}

?>

Output:

print_r($arr);

Array (
  [0] => Array (
    [name] => Fname Lname
    [email] => [email protected]
  )
  [1] => Array (
    [name] => Fname Lname
    [email] => [email protected]
  )
) 

Upvotes: 1

Daedalus
Daedalus

Reputation: 7722

The following regular expression will create an array with both the names, and the emails in sub arrays, in the keys [1] and [2] respectively:

<?php

$data = '"Fname Lname" <[email protected]>, "Fname Lname" <[email protected]>,';
preg_match_all('/\"(.*?)\"\s\<(.*?)\>/', $data, $arr, PREG_PATTERN_ORDER);
print_r($arr);

?>

As for email validation, in php 5.2 and up you can use filter_var($variable, FILTER_ID); specifically:

if (filter_var($email_string, FILTER_VALIDATE_EMAIL)) { 
    // do stuff
}

Described here.

Upvotes: 1

Estefano Salazar
Estefano Salazar

Reputation: 419

preg_match("/<[a-zA-Z0-9\\-_\\.]@[a-zA-Z0-9\\-]\\.([a-z]{3})(\\.[a-z]{2})>/i", "", $emails);

Greetings,

Upvotes: 0

Related Questions