In PHP, how do I extract multiple e-mail addresses from a block of text and put them into an array?

0 votes
asked Oct 10, 2010 by humblehelper

I have a block of text from which I want to extract the valid e-mail addresses and put them into an array. So far I have...

   $string = file_get_contents("example.txt"); // Load text file contents
   $matches = array(); //create array
   $pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/'; //regex for pattern of e-mail address
   preg_match($pattern, $string, $matches); //find matching pattern

However, I am getting an array with only one address. Therefore, I am guessing I need to cycle through this process somehow. How do I do that?

7 Answers

0 votes
answered Oct 10, 2010 by stevendesu

Your code is almost perfect, you just need to replace preg_match(...) with preg_match_all(...)

0 votes
answered Oct 10, 2010 by eric-karl

I know this is not the question you asked but I noticed that your regex is not accepting any address like '' or any address with a subdomain. You could replace it with something like :


which will reject less valid e-mail (although it is not perfect).

I also suggest you read this article on e-mail validation, it is pretty good and informative.

0 votes
answered Oct 10, 2010 by clay-hinson

You're pretty close, but the regex wouldn't catch all email formats, and you don't need to specify A-Za-z, you can just use the "i" flag to mark the entire expression as case insensitive. There are email format cases that are missed (especially subdomains), but this catches the ones I tested.

$string = file_get_contents("example.txt"); // Load text file contents

// don't need to preassign $matches, it's created dynamically

// this regex handles more email address formats like, and the i makes it case insensitive
$pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';

// preg_match_all returns an associative array
preg_match_all($pattern, $string, $matches);

// the data you want is in $matches[0], dump it with var_export() to see it


array (
  0 => '',
  1 => '',
  2 => '',
  3 => '',
  4 => '',
0 votes
answered Oct 4, 2014 by t-todua

This detects all mail addresses:

$sourceeee= 'Here are examplr and or something more';

preg_match_all('/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/i', $sourceeee, $found_mails);

then you can use $found_mails[0] array.

0 votes
answered Oct 26, 2014 by rebule

This regex will extract all unique email address from a url or file and output each in new line. It will consider all subdomains and prefix suffix issues. Find comfortable to use it.

$res = preg_match_all(
if ($res) {
foreach(array_unique($matches[0]) as $email) {
echo $email . "<br />";
else {
echo "No emails found.";

check here for more reference :

0 votes
answered Oct 30, 2015 by luis-rodriguez

It worked better for me:

$content = "Hi my name is Joe, I can be contacted at";
preg_match("/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/i", $content, $matches);
print $matches[0];

Some of the others didn't accept domains like:

I found it on:

0 votes
answered Sep 15, 2017 by alfatih

This function works fine without using regex. So it is really faster and low resource hungry.

function extract_email_addresses($str){
    $emails = array();
    $str = strip_tags( $str );
    $str = preg_replace('/\s+/', ' ', $str); 
    $str = preg_replace("/[\n\r]/", "", $str); 
    $remove_chars = array (',', "<", ">", ";", "'", ". ");
    $str = str_replace( $remove_chars, ' ', $str );
    $parts = explode(' ', $str);
    if(count($parts) > 0){
        foreach($parts as $part){
            $part = trim($part);
            if( $part != '' ) {
                if( filter_var($part, FILTER_VALIDATE_EMAIL) !== false){
                    $emails[] = $part;
    if(count($emails) > 0){
        return $emails;
        return null;

$string = "Guys, please help me to extract valid email addresses from some text content using php
example , i have below text content in mysql database ' Life is more beautiful, and i like to explore lot please email me to Learn new things every day. 'from the above text content i want to extract email address '' using php regular expressions or other method.";

$matches = extract_email_addresses( $string );

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter