Jump to content

Count each email in a text box/input field


Xtnct00WS6

Recommended Posts

I have a text box and I'm trying to get the number of actual email addresses that are in it. Problem is the format. Sometimes the input box has:

 

"john smith" <jonnyboy@gmail.com>

 

sometimes it's

 

"jonnyboy@gmail.com" <jonnyboy@gmail.com>.

 

sometimes it's just the email address itself "jonnyboy@gmail.com",

 

I have a function to strip out the < > and " " but how do I strip it out of each one? Here's the code:

echo  $firstpart;  // shows Heidi Klum Supermodel 
echo "<br>"; 
echo  $secondpart;  // shows info@HeidiKlum.com     
$firstpart = extractBetweenDelimeters($Recipients,"\"","\""); 
$secondpart = extractBetweenDelimeters($Recipients,"<",">");   
       
function extractBetweenDelimeters($inputstr,$delimeterLeft,$delimeterRight) {     
$posLeft  = stripos($inputstr,$delimeterLeft)+1; //posLeft is returning a number for the character that's the left of <     
$posRight = stripos($inputstr,$delimeterRight,$posLeft+1);         
$each = substr($inputstr,$posLeft,$posRight-$posLeft);     
return $each;    
} 

 

A likely string of emails would be:

"Bobeee Bob" <Bob@gmail.com>, "Ron@hotmail.com" <Ron@hotmail.com>, "johnee@ruckus.com" <johnee@ruckus.com>

Link to comment
Share on other sites

this will get all the email addresses in the input string and remove duplicates:

<?php

function getEmails($intputStr) {
  preg_match_all('/[\w\+-]+([\.]?[\w\+-])*@[a-zA-Z0-9]{2,}([\.-][a-zA-Z0-9]{2,})*\.[a-zA-Z]{2,4}/', $intputStr, $matches);
  $emailListAry = array_unique ($matches[0]);
  return $emailListAry;
}

$textInput = '"Bobeee Bob" <Bob@gmail.com>, "Ron@hotmail.com" <Ron@hotmail.com>, "johnee@ruckus.com" <johnee@ruckus.com>';
$emails = getEmails($textInput);

print_r($emails);

//OUTPUT:
//Array
//(
//    [0] => Bob@gmail.com
//    [1] => Ron@hotmail.com
//    [3] => johnee@ruckus.com
//)

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.