Last active
December 16, 2015 12:19
-
-
Save Kedrigern/5433813 to your computer and use it in GitHub Desktop.
Extract mail address in pointy brackets ("<", ">") from given string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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