Skip to content

Instantly share code, notes, and snippets.

@Kedrigern
Last active December 16, 2015 12:19
Show Gist options
  • Select an option

  • Save Kedrigern/5433813 to your computer and use it in GitHub Desktop.

Select an option

Save Kedrigern/5433813 to your computer and use it in GitHub Desktop.
Extract mail address in pointy brackets ("<", ">") from given string
<?php
/**
*
* Extract mail address in pointy brackets ("<", ">") from given string
*
* @author: Ondřej Profant <ondrej.profant <> gmail.com>, 2013
*
*/
function extractMail($text)
{
/**
* Regexp pattern definition
*/
static $split = '/[\<\>]/';
static $mail = '/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/';
$unique = array();
//$all = array();
// Split in the array by pointy brackets
$arr = preg_split($split, $text, -1, PREG_SPLIT_NO_EMPTY);
// Cyklus by parts of one global input string
foreach($arr as $a) {
// If match this is requested mail address
if( preg_match($mail, $a) ) {
//$unique[$a] = $a;
$all[] = $a;
// Print all matched values
//echo( "\t" . $a . "\n" );
}
}
if( empty( $all ) ) {
return false;
}
return $all;
}
/**
* Testing data
*/
$data = array (
"lorem ipsum",
"lorem @ ipsum",
"<lorem @ ipsum>",
"lorem@ ipsum",
"<lorem@ ipsum>",
"lorem @ipsum",
"<lorem @ipsum>",
"lorem@ipsum",
"<lorem@ipsum>",
"<mail@domain.com>",
"<mail@domain.com> <mail2@domain.com>",
"<mail@domain.com> lorem ipsum\n
lorem ipsum",
"lorem ipsum\n
lorem ipsum <mail@domain.com>",
"lorem ipsum <mail@domain.com> lorem ipsum",
"<mail@domain.com> <any bordel> <think> <tag attr=\"@\">",
"lorem mail@domain.cz lorem"
);
/**
* Regexp pattern definition
*/
$split = '/[\<\>]/';
$mail = '/[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}/';
$unique = array();
/**
* Testing data cyklus
*/
foreach($data as $d) {
print_r(extractMail($d));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment