Forked from vrushank-snippets/Extract emails from a string
Created
March 14, 2020 09:37
-
-
Save raihan-uddin/e399ffb238b80987ea19a70a9c49ff57 to your computer and use it in GitHub Desktop.
PHP : Extract emails from a string
This file contains 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
function extract_emails($str){ | |
// This regular expression extracts all emails from a string: | |
$regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'; | |
preg_match_all($regexp, $str, $m); | |
return isset($m[0]) ? $m[0] : array(); | |
} | |
$test_string = 'This is a test string... | |
test1@example.org | |
Test different formats: | |
test2@example.org; | |
<a href="test3@example.org">foobar</a> | |
<test4@example.org> | |
strange formats: | |
test5@example.org | |
test6[at]example.org | |
test7@example.net.org.com | |
test8@ example.org | |
test9@!foo!.org | |
foobar | |
'; | |
print_r(extract_emails($test_string)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment