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" <[email protected]>

 

sometimes it's

 

"[email protected]" <[email protected]>.

 

sometimes it's just the email address itself "[email protected]",

 

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 [email protected]     
$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" <[email protected]>, "[email protected]" <[email protected]>, "[email protected]" <[email protected]>

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" <[email protected]>, "[email protected]" <[email protected]>, "[email protected]" <[email protected]>';
$emails = getEmails($textInput);

print_r($emails);

//OUTPUT:
//Array
//(
//    [0] => [email protected]
//    [1] => [email protected]
//    [3] => [email protected]
//)

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.